Android 几种屏幕间跳转的跳转Intent Bundle
屏幕使用一个活动来实现,屏幕间是相互独立的,屏幕之间的跳转关系通过Intent来实现。
屏幕间跳转分为以下几类:
1. 屏幕1直接跳转到屏幕2
Intent intent = new Intent();
intent.setClass(屏幕1活动名.this,屏幕2活动名.class);
startActivity(intent);
finish(); //结束当前活动
2. 屏幕1带参数跳转到屏幕2
使用Bundle来传参数。
例子:猜拳游戏
界面:
重要代码:
电脑的选择是随机的,本次联系的基本思路是,三个选项利用三个数字来代替,让电脑 随机生成一个数字,根据数字的不同来产生不同的结果。
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, "请选择", Toast.LENGTH_LONG).show();
break;
}
skip();
}
//页面跳转
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="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="请选择您要出的拳:"
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, "请选择", 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("结果");
tv = (TextView)findViewById(R.id.show);
Bundle bundle = this.getIntent().getBundleExtra("result");
int playerInt = bundle.getInt("player");
int computerInt = bundle.getInt("computer");
tv.setText("猜拳结果\n");
tv.append("您的选择:");
intChangeString(playerInt);
tv.append("电脑的选择:");
intChangeString(computerInt);
tv.append("结果:");
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, "错误", Toast.LENGTH_LONG).show();
break;
}
}
}
3. 屏幕1跳转到屏幕2,屏幕2执行结束后有返回值到屏幕1(带返回值跳转)
参考示例程序:ReceiveResult(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得到从屏幕2返回的内容
@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 Activity中使用Intent实现页面跳转与参数传递的方法
- android中Intent传值与Bundle传值的区别详解
- android教程之intent的action属性使用示例(intent发短信)
- android中intent传递list或者对象的方法
- Android Intent的几种用法详细解析
- Android中使用IntentService创建后台服务实例
- Android 广播大全 Intent Action 事件详解
- Android Intent启动别的应用实现方法
- Android系列之Intent传递对象的几种实例方法
- 详解Android中Intent的使用方法
- Android编程中Intent实现页面跳转功能详解
- Android 几种屏幕间跳转的跳转Intent Bundle
- Android 几种屏幕间跳转的跳转Intent Bundle
- Android,使用Intent或Bundle传递参数,跳转页面。
- android基础之页面间跳转传值,用Bundle+Intent
- Android,使用Intent或Bundle传递参数,跳转页面
- Android,使用Intent或Bundle传递参数,跳转页面。
- Android,使用Intent或Bundle传递参数,跳转页面。
- android开发基础:Intent与Bundle的使用,实现页面跳转及信息传递
- Cannot resolve method 'putExtra(android.os.Bundle)' Intent界面跳转传值
- Android,使用Intent或Bundle传递参数,跳转页面
- Android Intent 的几种跳转
- android 关于intent传递类对象的几种方式
- android 数据传递详解(Serialization、Parcelable、Parcel、Intent、Bundle)
- Android中的Intent标准跳转应用
- Android开发实现的IntentUtil跳转多功能工具类【包含视频、音频、图片、摄像头等操作功能】
- Android初级开发第九讲--Intent最全用法(打开文件跳转页面等)
- android自学 intent 页面跳转的两种方法
- android锁屏状态下, 新消息唤醒屏幕,并跳转到指定页面
- androidManifest中配置launchMode和intent跳转时addFlag详解
- Android用Intent和Bundle传list