Android 幾種屏幕間跳轉(zhuǎn)的跳轉(zhuǎn)Intent Bundle
屏幕使用一個(gè)活動(dòng)來實(shí)現(xiàn),屏幕間是相互獨(dú)立的,屏幕之間的跳轉(zhuǎn)關(guān)系通過Intent來實(shí)現(xiàn)。
屏幕間跳轉(zhuǎn)分為以下幾類:
1. 屏幕1直接跳轉(zhuǎn)到屏幕2
Intent intent = new Intent();
intent.setClass(屏幕1活動(dòng)名.this,屏幕2活動(dòng)名.class);
startActivity(intent);
finish(); //結(jié)束當(dāng)前活動(dòng)
2. 屏幕1帶參數(shù)跳轉(zhuǎn)到屏幕2
使用Bundle來傳參數(shù)。
例子:猜拳游戲
界面:
重要代碼:
電腦的選擇是隨機(jī)的,本次聯(lián)系的基本思路是,三個(gè)選項(xiàng)利用三個(gè)數(shù)字來代替,讓電腦 隨機(jī)生成一個(gè)數(shù)字,根據(jù)數(shù)字的不同來產(chǎn)生不同的結(jié)果。
public void onClick(View v) {
switch (radioGroup.getCheckedRadioButtonId()){
case R.id.stone:
player = 0;
break;
case R.id.scissors:
player = 1;
break;
case R.id.textile:
player = 2;
break;
default:
Toast.makeText(MainActivity.this, "請(qǐng)選擇", Toast.LENGTH_LONG).show();
break;
}
skip();
}
//頁面跳轉(zhuǎn)
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);
}
跳轉(zhuǎn)之后,要接受參數(shù):
Bundle bundle = this.getIntent().getBundleExtra("result");
int playerInt = bundle.getInt("player");
int computerInt = bundle.getInt("computer");
猜拳游戲完整代碼:
activity_first.xml代碼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://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="請(qǐng)選擇您要出的拳:"
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:textSize="20dip"
android:text="剪刀" />
<RadioButton
android:id="@+id/bu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dip"
android:text="布" />
</RadioGroup>
<Button
android:id="@+id/chuquan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dip"
android:text="出拳" />
</LinearLayout>
activity_second.xml代碼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id ="@+id/show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dip"
android:text="@string/hello_world" />
</LinearLayout>
firstActivity.java代碼
package com.example.caiquangame;
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
public class firstActivity extends Activity {
private Button chuquan;
private RadioGroup quans;
private int player;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
setTitle("猜拳游戲");
chuquan = (Button)findViewById(R.id.chuquan);
chuquan.setOnClickListener(mChuQuanListener);
quans = (RadioGroup)findViewById(R.id.quans);
}
private OnClickListener mChuQuanListener = new OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
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, "請(qǐng)選擇", Toast.LENGTH_LONG).show();
break;
}
//將的到的值傳給secondActivity
skip();
}
};
private void skip()
{
Intent intent = new Intent();
intent.setClass(firstActivity.this, secondActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("player", player);
bundle.putInt("computer", new Random().nextInt(3));
intent.putExtra("result", bundle);
startActivity(intent);
}
}
secondActivity.java代碼
package com.example.caiquangame;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
public class secondActivity extends Activity {
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
setTitle("結(jié)果");
tv = (TextView)findViewById(R.id.show);
Bundle bundle = this.getIntent().getBundleExtra("result");
int playerInt = bundle.getInt("player");
int computerInt = bundle.getInt("computer");
tv.setText("猜拳結(jié)果\n");
tv.append("您的選擇:");
intChangeString(playerInt);
tv.append("電腦的選擇:");
intChangeString(computerInt);
tv.append("結(jié)果:");
if(playerInt == 0)
{
if(computerInt == 0)
{
tv.append("平局");
}
else if(computerInt == 1)
{
tv.append("您是贏家");
}
else
{
tv.append("電腦是贏家");
}
}
else if(playerInt == 1)
{
if(computerInt == 0)
{
tv.append("電腦是贏家");
}
else if(computerInt == 1)
{
tv.append("平局");
}
else
{
tv.append("您是贏家");
}
}
else
{
if(computerInt == 0)
{
tv.append("您是贏家");
}
else if(computerInt == 1)
{
tv.append("電腦是贏家");
}
else
{
tv.append("平局");
}
}
}
private void intChangeString(int n)
{
switch (n)
{
case 0:
tv.append("石頭\n");
break;
case 1:
tv.append("剪刀\n");
break;
case 2:
tv.append("布\n");
break;
default:
Toast.makeText(secondActivity.this, "錯(cuò)誤", Toast.LENGTH_LONG).show();
break;
}
}
}
3. 屏幕1跳轉(zhuǎn)到屏幕2,屏幕2執(zhí)行結(jié)束后有返回值到屏幕1(帶返回值跳轉(zhuǎn))
參考示例程序:ReceiveResult(ApiDemo => App=>Activity=>ReceiveResult)
重要代碼:
//屏幕1調(diào)轉(zhuǎn)到屏幕2
Intent intent = new Intent(Forward.this,ForwardTargetActivity.class);
startActivityForResult(intent, GET_CODE);
//在屏幕2設(shè)置返回值
setResult(RESULT_OK,(new Intent()).setAction("Violet!"));
finish();
//在屏幕1得到從屏幕2返回的內(nèi)容
@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");
}
- Android中傳值Intent與Bundle的區(qū)別小結(jié)
- android中Intent傳值與Bundle傳值的區(qū)別詳解
- Android 中為什么要用Fragment.setArguments(Bundle bundle)來傳遞參數(shù)
- Android 通過Intent使用Bundle傳遞對(duì)象詳細(xì)介紹
- 利用adt-bundle輕松搭建Android開發(fā)環(huán)境與Hello world(Linux)
- 利用adt-bundle輕松搭建Android開發(fā)環(huán)境與Hello world(Windows)
- Android開發(fā) Bundle傳值的理解與使用小結(jié)
相關(guān)文章
Activity配置、啟動(dòng)和關(guān)閉activity實(shí)例詳解
這篇文章主要介紹了Activity配置、啟動(dòng)和關(guān)閉activity實(shí)例詳解,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09Android Flutter實(shí)現(xiàn)點(diǎn)贊效果的示例代碼
點(diǎn)贊這個(gè)動(dòng)作不得不說在社交、短視頻等App中實(shí)在是太常見了。本文將利用Flutter制作出一個(gè)點(diǎn)贊動(dòng)畫效果,感興趣的小伙伴可以學(xué)習(xí)一下2022-04-04Android實(shí)現(xiàn)新手引導(dǎo)半透明蒙層效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)新手引導(dǎo)半透明蒙層效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03Android創(chuàng)建外部lib庫及自定義View的圖文教程
這篇文章主要給大家介紹了關(guān)于Android創(chuàng)建外部lib庫及自定義View的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11Android判斷是Wifi還是4G網(wǎng)絡(luò)代碼
這篇文章主要為大家詳細(xì)介紹了Android判斷網(wǎng)絡(luò)類型的方法,判斷是Wifi還是4G網(wǎng)絡(luò)代碼分享,感興趣的小伙伴們可以參考一下2016-07-07Android Webview上的ssl warning的處理方式詳解及實(shí)例
這篇文章主要介紹了Android Webview上的ssl warning的處理方式詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-02-02