Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 進階開發

Android 學習筆記:手把手實作 Activity 跳轉

編輯:進階開發

手機開發最重要的是使用者介面,而在 Android 中,Activity 正是應用與使用者溝通的介面!

學習 Android 開發之前,起碼要先學過 J2SE,因為我們是以 Java 語言來開發 Android 應用,自然得先具備 Java 基礎。學習 Java 的途徑很多,推薦在網上找一些好影片邊看邊學(筆者當初就是這樣入門的)。今天的任務是實作 Activity 跳轉(相當於 J2SE 中的介面切換)——這件事在 PC 上非常容易,但在 Android 手機上似乎還得多花一番功夫!

先來看看 Android 應用的目錄結構:src 目錄顧名思義,用來存放我們編寫的原始碼。gen 目錄需要特別留意,裡面有一個 R.java,用來定義各組件的 ID 值,一般不需要我們修改。assets 目錄可以放置一些資源檔案。接著是res 目錄,同樣用來存放資源檔案,但這裡的資源都必須在 R.java 中註冊 ID 值(一般是自動註冊的)。res 目錄下還有幾個子目錄:前三個是放圖片的(drawable-hdpi、drawable-ldpi、drawable-mdpi),分別代表不同解析度的圖片;layout 目錄存放布局檔案,這一項非常重要,我們會經常使用;還有一個 values 目錄,用來存放其他資源。需要特別注意的是,res 目錄及其子目錄下的檔案,都需要在 R.java 中註冊 ID 值。另外還有一個非常重要的檔案——Android 的設定檔 AndroidManifest.xml,我們建立的每一個 Activity 都必須在這裡配置。

下面來看開發實例。功能描述:第一個 Activity 中有一行文字和一個按鈕,點擊按鈕後,介面跳轉到第二個 Activity,並把從第一個 Activity 傳來的值顯示在螢幕上。以下是原始碼:

HelloActivity.java:

package guxia.android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class HelloActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView myTextView = (TextView) findViewById(R.id.myTextView);
        Button myButton = (Button) findViewById(R.id.myButton);

        myTextView.setText("welcome to myandroid");
        myButton.setText("my Button");

        myButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent();
                intent.putExtra("myname", "這是從 HelloActivity 傳過來的值");
                intent.setClass(HelloActivity.this, Activity01.class);
                HelloActivity.this.startActivity(intent);
            }
        });
    }
}

Activity01.java:

package guxia.android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class Activity01 extends Activity {
    private TextView myTextView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.android01);

        myTextView = (TextView) findViewById(R.id.android01TextView);
        Intent inte = getIntent();
        String myname = inte.getStringExtra("myname");
        myTextView.setText(myname);
    }
}

R.java:

/* AUTO-GENERATED FILE. DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found. It
 * should not be modified by hand.
 */

package guxia.android;

public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int icon = 0x7f020000;
    }
    public static final class id {
        public static final int android01TextView = 0x7f050000;
        public static final int myButton = 0x7f050002;
        public static final int myTextView = 0x7f050001;
    }
    public static final class layout {
        public static final int android01 = 0x7f030000;
        public static final int main = 0x7f030001;
    }
    public static final class string {
        public static final int android01 = 0x7f040002;
        public static final int app_name = 0x7f040001;
        public static final int hello = 0x7f040000;
    }
}

main.xml(layout 目錄下,HelloActivity 的布局檔案):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/myTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    <Button
        android:id="@+id/myButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>

Activity01.xml(Activity01 的布局檔案):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/android01TextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>

strings.xml(values 目錄下):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, HelloActivity!</string>
    <string name="app_name">hello Word</string>
    <string name="android01">android</string>
</resources>

設定檔 AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="guxia.android"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".HelloActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Activity01" android:label="@string/android01">
        </activity>
    </application>
</manifest>

整體流程其實很清晰:在 HelloActivity 中透過 Intent.putExtra() 放入資料,再用 setClass() 指定目標 Activity 並 startActivity() 啟動;到了 Activity01,則用 getIntent().getStringExtra() 取出資料並顯示。切記一定要在 AndroidManifest.xml 中註冊新的 Activity,否則程式執行時會出錯。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved