編輯:進階開發
在 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,兩端約定好同一組鍵名。
谷歌如何駕馭 Android 的雙面效應
Android 3.0 SDK 正式發布,Android 前幾個版本也已證明自己夠成功。但在全球成功背後,手機製造商對它的情感其實相當複雜:愛恨交織。 對 Dell、Ac
Eclipse 開發 Android 入門:重裝上陣(1)把泡茶計時器升級
在這個系列的第一部分,我們用 Android 與 Eclipse 做了一個簡單的泡茶計時器。第二部分要繼續開發它,加上幾個新功能,同時介紹更多重要而強大的 SDK 特性:
Android 的四種自由:開放、免費、可改與獨立執行緒
市面上的 Android 新聞從未間斷,全球粉絲更創造出數百款憨態可掬的機器人形象。而在熱鬧背後,微軟把自家手機系統統一改名為 Windows Phone;Google
Android 全家福:從 1.0 到 3.0 的發展歷程回顧
2010 年 5 月 20 日的 Google I/O 大會上,Android 2.2 Froyo 正式發布;同年 12 月 7 日凌晨一時,Android 2.3 Gi