編輯:關於 Android 程式設計
Activity 之間傳值,最正統的方式是 Bundle 搭配 Intent。它不只是「放進去、拿出來」這麼簡單,還牽涉到型別選擇、資料大小限制與生命週期。以下用一個「輸入兩個數字、跳到下一頁算總和」的例子完整走一遍。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="請輸入第一個數字:" />
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="請輸入第二個數字:" />
<EditText
android:id="@+id/editText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交" />
</LinearLayout>
加上 android:inputType="number" 可以讓使用者只能輸入數字並叫出數字鍵盤,省去後續的錯誤處理。
package com.liuhaoyu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText edit1 = (EditText) findViewById(R.id.editText1);
final EditText edit2 = (EditText) findViewById(R.id.editText2);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String num1 = edit1.getText().toString();
String num2 = edit2.getText().toString();
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putCharSequence("number1", num1);
bundle.putCharSequence("number2", num2);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
}
流程是:建立 Bundle → 用 putXxx() 放入成對的 key-value → 透過 intent.putExtras() 掛到 Intent 上 → startActivity()。
package com.liuhaoyu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String num1 = bundle.getString("number1");
String num2 = bundle.getString("number2");
int sum = Integer.parseInt(num1) + Integer.parseInt(num2);
TextView text = (TextView) findViewById(R.id.textView);
text.setText(num1 + " + " + num2 + " = " + String.valueOf(sum));
}
}
接收方的三個動作:getIntent() 拿到 Intent → getExtras() 取出 Bundle → 用 getString() 等對應方法依 key 取值。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="20dp" />
</LinearLayout>
<activity
android:icon="@drawable/ic_launcher"
android:name=".SecondActivity"
android:label="計算結果"
android:theme="@android:style/Theme.Dialog" >
</activity>
沒有這段宣告,startActivity() 會直接拋出 ActivityNotFoundException。android:theme="@android:style/Theme.Dialog" 讓這個 Activity 以對話框形式呈現,視覺上比較像彈窗而非整頁。
put 與 get 的 key 字串要完全相同,get 的方法也要與 put 的方法配對。putInt() 卻用 getString() 取,不會報錯但會拿到 null,接著 parseInt(null) 才爆掉——這類錯誤很難追。
如果只是單純傳值,可以跳過顯式建立 Bundle:
intent.putExtra("number1", num1);
intent.putExtra("number2", num2);
startActivity(intent);
// 接收端
String num1 = getIntent().getStringExtra("number1");
Bundle 的價值在於「需要一次打包多筆結構化資料」或「要存進 onSaveInstanceState()」時。
Activity 之間的通訊經過 Binder,而 Binder 的緩衝區只有約 1MB(且需與其他程序共用)。傳送大圖片或長清單會拋出 TransactionTooLargeException。大量資料應該改用其他途徑:傳 URI 指向檔案、透過 ViewModel 共享、或使用 Repository 層讓兩頁各自查詢。
Bundle 支援 putString / putInt / putBoolean / putParcelable / putSerializable 等。自訂物件要能傳遞,必須實作 Parcelable(推薦,效能好)或 Serializable(簡單但較慢)。
直接 Integer.parseInt(num1) 在使用者留空時會拋 NumberFormatException。正式版本應該先檢查空字串並給出提示。另外,用字串傳數字再轉回來繞了一圈,其實可以直接 bundle.putInt(),省去轉型的風險。
上面的做法是單向的:SecondActivity 算完就顯示在自己畫面上,MainActivity 不知情。若需要把結果送回上一頁,則要改用 startActivityForResult() 搭配 setResult(),這是另一個主題。
Android 實作:「抽獎大轉盤」教學(旋轉動畫 + 計時器)
這個範例可以幫助我們掌握兩個知識點:旋轉動畫與計時器。廢話不多說,直接上主程式: package com.bear.lotterywheel; import java.u
打造自訂縮放 ImageView(上):Matrix、手勢偵測與自由縮放
看小圖點開大圖、兩指放大、拖到想看的位置——這套操作已經是使用者的肌肉記憶。圖片預覽功能做不好,體驗立刻扣分。要自己實作,得先認識四個東西:Matrix、GestureD
Android 基礎入門教程 8.3.6:Paint API 之 Xfermode 與 PorterDuff 詳解(三)
本節引言 上一節我們認識了 PorterDuffXfermode 構造方法中的關鍵參數 PorterDuff.Mode,並把文件列出的混圖模式逐一驗證了一遍。除了官方文
Android 安全審計工具 MobiSecLab 上手實測
分析 Android 應用時,本地端有許多工具可以勝任。本文介紹的是另一條路——線上化的惡意軟體檢測與靜態分析平台 MobiSecLab。國內相關介紹不多,但實際使用量並