編輯:關於 Android 程式設計
在 Android 中,每個屏幕通常對應一個 Activity,各屏幕彼此獨立,而屏幕之間的跳轉則透過 Intent 來實現。本文以猜拳小遊戲為例,系統梳理頁面跳轉的三種典型場景。
Intent intent = new Intent();
intent.setClass(屏幕1活動名.this, 屏幕2活動名.class);
startActivity(intent);
finish(); //結束當前活動
使用 Bundle 來傳遞參數。以猜拳遊戲為例,用三個數字分別代表石頭、剪刀、布,讓電腦隨機生成一個數字產生對局結果。
界面:


發送方的核心代碼:
private void skip() {
Intent intent = new Intent();
intent.setClass(MainActivity.this, ResultMainActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("player", player);
bundle.putInt("computer", new Random().nextInt(3));
intent.putExtra("result", bundle);
startActivity(intent);
}
接收方取出參數:
Bundle bundle = this.getIntent().getBundleExtra("result");
int playerInt = bundle.getInt("player");
int computerInt = bundle.getInt("computer");
布局文件 activity_first.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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請選擇您要出的拳:"
android:textSize="20dip" />
<RadioGroup
android:id="@+id/quans"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton android:id="@+id/shitou" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="石頭" android:textSize="20dip" />
<RadioButton android:id="@+id/jiandao" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="剪刀" android:textSize="20dip" />
<RadioButton android:id="@+id/bu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="布" android:textSize="20dip" />
</RadioGroup>
<Button android:id="@+id/chuquan" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="出拳" android:textSize="20dip" />
</LinearLayout>
發送方 Activity(firstActivity.java)核心邏輯:
private OnClickListener mChuQuanListener = new OnClickListener() {
@Override
public void onClick(View arg0) {
switch (quans.getCheckedRadioButtonId()) {
case R.id.shitou: player = 0; break;
case R.id.jiandao: player = 1; break;
case R.id.bu: player = 2; break;
default: Toast.makeText(firstActivity.this, "請選擇", Toast.LENGTH_LONG).show(); break;
}
skip();
}
};
接收方 Activity(secondActivity.java)在 onCreate 中取出 Bundle 並比較雙方出拳結果,透過 tv.setText / tv.append 展示「您的選擇、電腦的選擇、最終結果」。判斷邏輯是對 playerInt 與 computerInt 的組合進行 if-else 分支處理,0/1/2 分別代表石頭/剪刀/布,相同為平局,否則依規則判定勝負。
參考 ApiDemo => App => Activity => ReceiveResult。核心代碼如下:
//屏幕1跳轉到屏幕2
Intent intent = new Intent(Forward.this, ForwardTargetActivity.class);
startActivityForResult(intent, GET_CODE);
//在屏幕2設置返回值
setResult(RESULT_OK, (new Intent()).setAction("Violet!"));
finish();
//在屏幕1接收返回內容
@Override
protected void onActivityResult(int RequestCode, int ResultCode, Intent data) {
if (RequestCode == GET_CODE) {
if (ResultCode == RESULT_CANCELED) {
edit.append("canceled!");
} else {
edit.append("(okay ");
edit.append(Integer.toString(ResultCode));
edit.append(")");
}
if (data != null) { edit.append(data.getAction()); }
}
edit.append("\n");
}
掌握這三種場景,就能應對絕大多數頁面跳轉與數據傳遞的需求。
《極簡筆記》原始碼分析(一):從 Manifest 到 MainActivity
《極簡筆記》是一個以 Material Design 與 MVP 架構實作的筆記應用。本文從 Manifest 開始,逐步分析 Application、Activity、
用程式碼安裝與卸載 Android APK 的完整實作
想在自己的 App 裡做「安裝」與「卸載」按鈕,靠的是 Intent 加上正確的 action 與 data。核心關鍵在這一行: intent.setDataAndTyp
Android View 體系:從源碼解析 Activity 的構成
在學習 View 工作流程前,需要先理解 Activity 的窗口與視圖結構。setContentView 並不是直接把布局放入 Activity,而是交給 Window
Android Support Library 常用控件詳解(下):BottomSheet、RecyclerView、CardView 等
Google 推出 Material Design 已經有一段時間,為了讓開發者更容易落地這套設計語言,官方提供了 support design library。順帶推薦