Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Activity 之間的資料傳遞:Intent 用法全解(附人品計算器實作)

編輯:關於 Android 程式設計

本文用一個「人品計算器」小專案,把 Activity 之間傳遞資料的常見做法完整跑一遍:首頁輸入姓名,按下按鈕後跳到結果頁,由結果頁隨機算出一個人品值並顯示出來。

Intent 傳值的幾種寫法

從 A 畫面開啟 B 畫面並把資料帶過去,主要有兩條路:

1. intent.setData(uri) / intent.getData():適合傳遞簡短的純文字或 URI 型資料。

2. intent.putExtra(name, value):適用範圍最廣,包括 8 種基本型別及其陣列、String、CharSequence、實作 Serializable 的物件(序列化到檔案)、實作 Parcelable 的物件(序列化到記憶體,Android 上更推薦)、Bitmap,也可以一次塞一整包 Bundle。

實務上 putExtra 用得最多,所以下面的範例就走這條路。

首頁布局 activity_main.xml

<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>

畫面如下:

結果頁布局 activity_result.xml

輸入姓名後按鈕,即跳到結果頁:

<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>

畫面如下:

ResultActivity.java

結果頁的關鍵就是 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。

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