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

用 Bundle 在多個 Activity 之間傳遞資料

編輯:進階開發

在 Activity 中呼叫另一個 Activity 時,往往還得把資料一起帶過去。這時可以借重 android.os.Bundle 的封裝能力,把要傳遞的內容裝進 Bundle,再掛到 Intent 上送往下一頁。

本範例做一個簡單的個人資料表單:一個姓名輸入框(EditText)、一個性別選單(RadioButton)和一顆提交鈕(Button)。按下提交後,另一個頁面會把填寫的內容顯示出來。

一、版面配置

主頁面 main.xml 這裡刻意選用 AbsoluteLayout,用不到版面自動排版,元素位置全靠座標指定:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Person information" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Name:"
        android:layout_y="30px" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Gender:"
        android:layout_y="80px" />

    <EditText
        android:id="@+id/ed1"
        android:layout_width="200px"
        android:layout_height="wrap_content"
        android:layout_x="50px"
        android:layout_y="20px" />

    <RadioButton
        android:id="@+id/rb1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="man"
        android:layout_x="60px"
        android:layout_y="65px"
        android:checked="true" />

    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="confirm"
        android:layout_y="120px" />
</AbsoluteLayout>

注意 android:checked="true" 讓單選鈕預設被選中。

在同一目錄下建立第二個頁面 mylayout.xml,用來顯示結果:

<?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/mytv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
</LinearLayout>

二、程式碼

核心在 BundleDemo.java:

package com.android.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class BundleDemo extends Activity {

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

        Button bt1 = (Button) findViewById(R.id.bt1);
        bt1.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                // 取得姓名
                EditText et = (EditText) findViewById(R.id.ed1);
                String name = et.getText().toString();

                // 取得性別
                String sex;
                RadioButton rb1 = (RadioButton) findViewById(R.id.rb1);
                sex = rb1.isChecked() ? "man" : "woman";

                // 建立 Intent,指定要啟動的 Class
                Intent intent = new Intent();
                intent.setClass(BundleDemo.this, BundleDemo1.class);

                // 建立 Bundle 並放入要傳遞的資料,結構等同 Map<String, String>
                Bundle bundle = new Bundle();
                bundle.putString("name", name);
                bundle.putString("sex", sex);

                // 把 Bundle 掛到 Intent 上
                intent.putExtras(bundle);

                startActivity(intent);
            }
        });
    }
}

接收端 BundleDemo1.java:

package com.android.test;

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

public class BundleDemo1 extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylayout);

        // 取出 Intent 裡的 Bundle
        Bundle bundle = this.getIntent().getExtras();
        String name = bundle.getString("name");
        String sex = bundle.getString("sex");

        TextView mytv = (TextView) findViewById(R.id.mytv);
        mytv.setText("you name is: " + name + " you gender is: " + sex);
    }
}

三、別忘了註冊

最後一定要在 AndroidManifest.xml 裡加上 BundleDemo1,否則執行時會找不到該元件而直接崩潰——這是新手最常踩的坑。

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

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".BundleDemo"
            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="BundleDemo1" />
    </application>

    <uses-sdk android:minSdkVersion="3" />
</manifest>

Bundle 的本質就是一組 key-value 對,putXxx 與 getXxx 的型別必須成對出現,否則會拿到 null 或直接拋出例外。當要傳的資料結構變複雜時,可以改用 putParcelable 或實作 Serializable 的物件,但 principle 不變:先裝進 Bundle,掛上 Intent,兩端約定好同一組鍵名。

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