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

Android 畫面切換實作:用 Intent 跳轉並帶值過去

編輯:初階開發

在 Android 裡,一個 Activity 就是一個獨立畫面。畫面之間的切換很類似網頁上的超連結——按下某個按鈕,就跳到另一個畫面。負責這件事的,是名為 Intent 的物件,意思正是「意圖」:你想做什麼,就描述給系統聽。

下面用登入流程做示範:在首頁輸入帳號密碼,按下登入後跳到第二個畫面,並把輸入的內容顯示出來。

出發頁:Activity1.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button btnLogin = (Button) findViewById(R.id.btn_login);
    btnLogin.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            String userName = ((EditText) findViewById(R.id.et_name)).getText().toString();
            String pwd = ((EditText) findViewById(R.id.et_pwd)).getText().toString();

            Intent intent = new Intent();
            intent.setClass(Activity1.this, LoginOK.class);   // 指定從哪裡跳到哪裡

            Bundle bundle = new Bundle();                     // 傳值用的容器,用法近似 Map
            bundle.putString("userName", userName);
            bundle.putString("pwd", pwd);
            intent.putExtras(bundle);                         // 把 Bundle 放進 Intent

            startActivity(intent);                            // 執行跳轉
        }
    });
}

目的地:LoginOK.java

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

    // 取得上一頁傳來的 Bundle
    Bundle bundle = this.getIntent().getExtras();

    TextView okInfo = (TextView) findViewById(R.id.okinfo);
    StringBuilder sb = new StringBuilder();
    sb.append("登錄資訊為:\n");
    sb.append("使用者名稱:");
    sb.append(bundle.getString("userName"));   // 以 key 取值
    sb.append("\n");
    sb.append("密碼:");
    sb.append(bundle.getString("pwd"));
    sb.append("\n");
    okInfo.setText(sb.toString());
}

別忘了註冊新 Activity

Android 應用預設只有一個 Activity,多一個就必須在 AndroidManifest.xml 裡宣告,否則跳轉時會丟出 ActivityNotFoundException:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="test.k"
    android:versionCode="1"
    android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Test1"
            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 -->
        <activity android:name="LoginOK" android:label="登錄結果顯示畫面"></activity>
    </application>
</manifest>

幾個觀念補充

setClass 與 setClassName。前者用 Class 物件指定目標,比較直覺;若跨套件呼叫,則用 setClassName(套件名, 類別名)。兩者都屬於「顯式 Intent」,也就是明確點名要開哪一個畫面。

Bundle 的限制。它只能裝基本型別、字串與實作 Parcelable/Serializable 的物件。若要傳自訂物件,記得實作 Parcelable。

取 Bundle 前先判空。getExtras() 有可能回傳 null,正式環境建議寫成 bundle != null ? bundle.getString("userName") : "",避免空指針。

AndroidManifest.xml 這個檔案的重要性遠大於它看起來的樣子——權限、Service、Receiver、Provider 全都在這裡宣告,值得另外花時間好好讀一遍。

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