編輯:關於 Android 開發
本文透過一個簡易的「人品計算器」小程式,實作從 A 畫面跳轉到 B 畫面並把資料帶過去的效果。
從 A 介面打開 B 介面,把 A 的資料傳給 B,主要有兩種做法:
方式一:intent.setData(uri) / intent.getData()
適合傳遞簡單的 URI 型資料,例如 content://、https:// 之類的資源位址。
方式二:intent.putExtra(name, value)
最常用,可傳遞的型別很廣:
SerializableParcelableBitmap 等圖形物件Bundle(intent.putExtra("key", bundle))實務上效能優先選 Parcelable,它在 Android 中的序列化速度明顯優於 Serializable。
本文來源:cnblogs.com/wuyudong
<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="請輸入姓名" >
</EditText>
<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="計算" >
</Button>
</RelativeLayout>
</LinearLayout>
介面如下:

輸入姓名後點擊按鈕,帶著資料跳到結果頁:
<?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: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>
介面如下:

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);
// 關鍵:用 getIntent() 取得呼叫方帶來的資料
Intent intent = getIntent();
String name = intent.getStringExtra("name");
Random rm = new Random();
int rp = rm.nextInt(101);
tv_result.setText(name + ":您的人品值為:" + rp);
ProgressBar pb = (ProgressBar) findViewById(R.id.progressBar1);
pb.setProgress(rp);
}
}
在 MainActivity 中補上按鈕回呼,用 putExtra 放入資料後啟動目標 Activity:
public void click(View v) {
EditText etName = (EditText) findViewById(R.id.et_name);
String name = etName.getText().toString().trim();
Intent intent = new Intent(this, ResultActivity.class);
intent.putExtra("name", name); // key 必須與接收端一致
startActivity(intent);
}
public static final String EXTRA_NAME = "name" 放在目標 Activity 中,避免字串打錯。TransactionTooLargeException。傳大圖請改用 URI 或先存到檔案/Repository。startActivityForResult() 搭配 setResult()(或新的 Activity Result API)。
我的 Android 學習筆記(26):ViewPager 分頁導航完整實作
我的 Android 學習筆記(26):ViewPager 分頁導航完整實作 用過微信或 QQ 的人,對頂部那排可以左右滑動切換的導航欄一定不陌生。要在 Android
Android 應用集成 Facebook 登錄的入門指引
第一步:注册 Facebook 開發者賬號 要在 Android 應用中集成 Facebook 登錄,首先需要前往 Facebook 開發者平台注册一個賬號: https
ListView 與 Adapter 筆記:解讀 ZrcListView 的原始碼
ListView 與 Adapter 筆記:ZrcListView 原始碼解析 專案地址:https://github.com/zarics/ZrcListView 先貼
MediaPlayer 進階實戰:用 SurfaceView 打造網絡視頻播放器
上一篇我們把 MediaPlayer 的網絡音頻播放梳理了一遍,從數據緩衝到進度條同步都走通了流程。這一篇順著同一條思路往前推進一步——把播放對象從聲音換成畫面。 說穿