前言
看到這篇文章時代表你(妳)對Android開發有初步概念,如果沒有可先參考【從1到2】,相信大家一開始一定會遇到的兩個問題是,一、要如何切換到Activity 二、要怎麼把參數帶要下一個Activity,在此提供方法給大家參考。
1.Intent
Intent 是一個非常好用的東西,例如要實作電話撥打、打開相機…等,如果有持續開發之後一定會常常用到,當然Intent 也提供切換Activity功能。代碼如下:
1 2 3 4 5 6 7 8 |
// 從 MainActivity 切換到 Test Intent intent = new Intent(); intent.setClass(MainActivity.this,Test.class); //告訴它從哪邊切換到哪邊 startActivity(intent);//切換 or Intent intent = new Intent(MainActivity.this,Test.class); startActivity(intent);//切換 |
2.Bundle
Bundle 主要用於資料傳遞,主要是以key-value方式來儲存資料。代碼如下:
1 2 3 4 5 |
//實例化一個Bundle物件 Bundle bundle = new Bundle(); //儲存資料 第一個為參數key,第二個為Value bundle.putString("key","Value"); bundle.putString("name","learnexp"); |
3.Intent +Bundle合併運用
1 2 3 4 5 6 7 |
Intent intent = new Intent(); intent.setClass(MainActivity.this,Test.class); Bundle bundle = new Bundle(); bundle.putString("key","Value"); bundle.putString("name","learnexp"); intent.putExtras(bundle); // 記得put進去,不然資料不會帶過去哦 startActivity(intent); |
MainActivity 把值帶到 Test,讀取Bundle代碼如下:
1 2 3 4 |
//取值方法 Bundle bundle = getIntent().getExtras(); String key = bundle.getString("key"); String name = bundle.getString("name"); |
4.實際演練
延用【計算BMI値】範例,修改為MainActivity(起始頁)提供輸入身高、體重,另新增一個Layout CountBMI 顯示BMI値結果。
activity_main.xml中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="身高:" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tv1" android:layout_below="@+id/tv1" android:layout_marginTop="15dp" android:text="體重:" android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText android:id="@+id/et1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/tv1" android:layout_marginLeft="15dp" android:layout_toRightOf="@+id/tv1" android:ems="10"/> <EditText android:id="@+id/et2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/et1" android:layout_below="@+id/et1" android:ems="10" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/et2" android:layout_below="@+id/et2" android:layout_marginTop="21dp" android:text="送出" /> </RelativeLayout> |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
public class MainActivity extends AppCompatActivity { EditText h; //宣告全域變數 EditText w; //宣告全域變數 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); h = (EditText)findViewById(R.id.et1); // 取得身高物件 w = (EditText)findViewById(R.id.et2); // 取得體重物件 Button submit = (Button)findViewById(R.id.button1); // 取得按鈕物件 // 按下按鈕 觸發事件 submit.setOnClickListener(new Button.OnClickListener() { public void onClick(View arg0) { //判斷條件 身高 跟 體重 都有輸入值才執行 if ( !("".equals(h.getText().toString()) || "".equals(w.getText().toString())) ) { Intent intent = new Intent(); intent.setClass(MainActivity.this,CountBMI.class); Bundle bundle = new Bundle(); bundle.putString("height",h.getText().toString()); bundle.putString("width",w.getText().toString()); intent.putExtras(bundle); // 記得put進去,不然資料不會帶過去哦 startActivity(intent); } } }); } } |
activity_count_bmi.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="test.com.myapplication.CountBMI"> <TextView android:id="@+id/tv3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignRight="@+id/button1" android:layout_below="@+id/button1" android:text="結果:" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/tv4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignRight="@+id/button1" android:layout_below="@+id/tv3" android:layout_marginTop="10dp" android:text="診斷:" android:textAppearance="?android:attr/textAppearanceMedium" /> </RelativeLayout> |
CountBMI.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
public class CountBMI extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_count_bmi); Bundle bundle = getIntent().getExtras(); String h = bundle.getString("height" ); String w = bundle.getString("width"); float fh = Float.parseFloat(h); // 取得 身高輸入值 float fw = Float.parseFloat(w); // 取得 體重輸入值 float fresult; // BMI值 計算結果 TextView result = (TextView)findViewById(R.id.tv3);// 取得 顯示結果 物件 fh = fh/100; // 計算BMI fh = fh*fh; // 計算BMI NumberFormat nf = NumberFormat.getInstance(); // 數字格式 nf.setMaximumFractionDigits(2); // 限制小數第二位 fresult = fw/fh; // 計算BMI result.setText(nf.format(fw/fh) ); // 顯示BMI計算結果 TextView dia = (TextView)findViewById(R.id.tv4);// 取得 顯示診斷 物件 // 診斷結果 顯示 if (fresult<18.5) dia.setText("體重過輕"); else if (18.5 <= fresult && fresult< 24) dia.setText("正常範圍"); else if (24 <=fresult && fresult < 27) dia.setText("過 重"); else if (27 <=fresult && fresult < 30) dia.setText("輕度肥胖"); else if (30 <= fresult && fresult < 35) dia.setText("中度肥胖"); else if (fresult >= 35) dia.setText("重度肥胖 "); } } |
演練結果
Exp Up
Global Variable 共用變數 從 3到4 – 如果每個Activity都要用到的參數可以利用共用變數來存儲
文中好像沒有提到要在 AndroidManifest.xml 中加上
app才不會在按下按鈕後崩潰
感謝你的提醒,基本上New Activity系統會自己再AndroidManifest.xml加入activity宣告,但如果是各別New Java和Layout就必須手動設定AndroidManifest.xml
請問要如何修改?
謝謝
想請問版主
在按下按鈕後發生app崩潰
要如何處理呢? 感謝~~
再AndroidManifest.xml加入activity宣告,如
activity android:name=”.CountBMI”
感謝支援
請問一下
假如我有3個Activity(A/B/C)
同一筆資料要從ActivityA到ActivityB再到ActivityC最後回到ActivityC
我的intent跟bundle是要在ActivityA跟ActivityB各New一次?還是可以共用?
A到B 寫一次,B到C寫一次,C到A寫一次。
如果你想共用可以參考
Global Variable 共用變數 從 3到4
Shared Preferences 簡易型資料庫
還有人能幫解答嗎