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

Android 頁面跳轉詳解:Intent 與 Bundle 的幾種用法

編輯:關於 Android 程式設計

在 Android 中,每個屏幕通常對應一個 Activity,各屏幕彼此獨立,而屏幕之間的跳轉則透過 Intent 來實現。本文以猜拳小遊戲為例,系統梳理頁面跳轉的三種典型場景。

場景一:屏幕 1 直接跳轉到屏幕 2

Intent intent = new Intent();
intent.setClass(屏幕1活動名.this, 屏幕2活動名.class);
startActivity(intent);
finish();  //結束當前活動

場景二:屏幕 1 帶參數跳轉到屏幕 2

使用 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 分別代表石頭/剪刀/布,相同為平局,否則依規則判定勝負。

場景三:帶返回值跳轉(startActivityForResult)

參考 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");
}

掌握這三種場景,就能應對絕大多數頁面跳轉與數據傳遞的需求。

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