編輯:關於 Android 程式設計
本文用一個「人品計算器」小專案,把 Activity 之間傳遞資料的常見做法完整跑一遍:首頁輸入姓名,按下按鈕後跳到結果頁,由結果頁隨機算出一個人品值並顯示出來。
從 A 畫面開啟 B 畫面並把資料帶過去,主要有兩條路:
1. intent.setData(uri) / intent.getData():適合傳遞簡短的純文字或 URI 型資料。
2. intent.putExtra(name, value):適用範圍最廣,包括 8 種基本型別及其陣列、String、CharSequence、實作 Serializable 的物件(序列化到檔案)、實作 Parcelable 的物件(序列化到記憶體,Android 上更推薦)、Bitmap,也可以一次塞一整包 Bundle。
實務上 putExtra 用得最多,所以下面的範例就走這條路。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="人品計算器"
android:textColor="#ff0000"
android:textSize="26sp" />
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入姓名" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="click"
android:text="計算" />
</RelativeLayout>
</LinearLayout>
畫面如下:

輸入姓名後按鈕,即跳到結果頁:
<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:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="測試結果:"
android:textColor="#ff0000"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="xxx:"
android:textColor="#99000000"
android:textSize="17sp" />
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="100" />
</LinearLayout>
畫面如下:

結果頁的關鍵就是 getIntent() 加上 getStringExtra():把 Intent 拿回來,用出發頁約定好的 key 取出資料,剩下的就是一般的 UI 更新。
package com.wuyudong.testrp;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.TextView;
public class ResultActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
TextView tv_result = (TextView) findViewById(R.id.tv_result);
// 用 putExtra 時約定的 key 把資料取出來
Intent intent = getIntent();
String name = intent.getStringExtra("name");
// 隨機產生 0~100 的人品值
Random rm = new Random();
int rp = rm.nextInt(101);
tv_result.setText(name + ":您的人品值為:" + rp);
ProgressBar pb = (ProgressBar) findViewById(R.id.progressBar1);
pb.setProgress(rp);
}
}
出發頁那邊則對應寫成 intent.putExtra("name", 輸入的名字) 後接 startActivity(intent)。整組流程就是:putExtra 打包 → startActivity 出發 → getIntent 收貨 → getXxxExtra 拆包。
補充一個實務提醒:如果要在 Activity 之間回傳結果(例如 B 處理完把結果送回 A),改用 startActivityForResult 搭配 setResult + onActivityResult 會更合適;而元件之間傳遞較複雜的結構時,優先考慮 Parcelable,效能明顯優於 Serializable。
Android 音樂播放 Service 實作教學
Service 是 Android 中常見的背景元件,適合用來執行不需要與使用者直接互動的長時間工作。本文以音樂播放器為例,說明如何利用 Service 播放背景音樂,並
Android 實現畫板/寫字板功能(附源碼下載)
本文分享一個用 Android 實現簡單寫字板的完整示例。原理並不複雜:把一張圖片解碼成可變的 Bitmap 副本,關聯到 Canvas 上,然後根據手指的移動軌跡不斷畫
Android 學習筆記:ListView 的簡單使用
(1)佈局檔中加入一個 ListView: (2)在 MainActivity 中設定選擇模式、綁定 ArrayAdapter,並處理項目的點擊事件:package
Picasso 在 Android 中的圖片載入與快取機制解析
Picasso 是 Square 開源的 Android 圖片載入庫,主打簡潔 API 與內建快取。只要一行呼叫,就能完成網路圖片的異步載入與顯示。 Picasso.wi