屏幕使用一个活动来实现,屏幕间是相互独立的,屏幕之间的跳转关系通过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代码

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="请选择您要出的拳:"

android:textSize="20dip" />

android:id="@+id/quans"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:id="@+id/shitou"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:checked="true"

android:text="石头"

android:textSize="20dip" />

android:id="@+id/jiandao"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="20dip"

android:text="剪刀" />

android:id="@+id/bu"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="20dip"

android:text="布" />

android:id="@+id/chuquan"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="20dip"

android:text="出拳" />

activity_second.xml代码

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id ="@+id/show"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="20dip"

android:text="@string/hello_world" />

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屏幕跳转,Android 几种屏幕间跳转的跳转Intent Bundle相关推荐

  1. 三星手机连接服务器显示1104,小米10手机三星屏和国产屏混用?两种屏幕有差别吗?...

    2020年2月13日,小米十周年之际发布了新一代小米数字系列旗舰机小米10,该机是首发骁龙865处理器的的智能手机,性能方面肯定是无瑕疵可挑剔,而让人们议论纷纷的是小米10所采用的屏幕,大量采用三星屏 ...

  2. html js页面跳转的几种方式

    页面跳转的几种方式 一:js的跳转 1.直接跳转:window.location.href <script language="javascript" type=" ...

  3. android设置布局高度自适应,4种Android屏幕自适应解决方案

    Android支持多屏幕机制即用为当前设备屏幕提供一种合适的方式来共同管理并解析应用资源.本文就介绍了4中Android屏幕自适应解决方案. 一.细说layout_weight 目前最为推荐的Andr ...

  4. Android保持屏幕常亮的两种方法

    保持屏幕常亮的两种方法: 我们大家在用手机的时候,会遇到这样的一个问题,就是想让我们的手机屏幕一直亮着怎么办.大家会想到的是,在手机设置里有一个不省电模式,选择这个就可以了,但是我们要在代码中是怎么样 ...

  5. android 屏幕录制方案,Android录屏的三种解决方案

    本文总结三种用于安卓录屏的解决方案: adb shell命令screenrecord MediaRecorder, MediaProjection MediaProjection , MediaCod ...

  6. android 屏幕录制方案,Android录屏的三种方案

    本文总结三种用于安卓录屏的解决方案: adb shell命令screenrecord MediaRecorder, MediaProjection MediaProjection , MediaCod ...

  7. android 特殊屏幕适配,Android屏幕适配(2)常见:第一种

    前言: Android屏幕适配(1)基础知识篇 Android屏幕适配(3)常见:第二种 Android屏幕适配(4)常见:第三种 Android屏幕适配(5)常见:归纳总结 常见的屏幕配置问题(一些 ...

  8. android的设计规范了解,Android设计规范常识:四种屏幕尺寸和四种密度

    看到上面这个标题:肯定知道xd素材中文网这节课需要跟大家聊的大概主题了吧! 没错,就是Android设计规范当中必须了解的四种屏幕尺寸和四种密度分类.为什么只有Android界面上会产生四种屏幕尺寸和 ...

  9. android 关于屏幕截屏的几种办法

    年末较闲,就上个星期查找各方面的资料关于android截图事情,自已也测试一些代码,已改改进或者优化.接下来进行总结一下.其实,如果真正android系统截屏是需要root权限的.但要用户使用都roo ...

最新文章

  1. 使用AppleScript播放指定时间的电影片段
  2. INPUT type=password 元素 | input type=password 对象
  3. 数据分析案例(贷款风险预测)
  4. dbscan聚类算法matlab_密度聚类DBSCAN、HDBSCAN(转)
  5. project 模板_施工进度横道图模板,全套电子版,工作效率大大提高!
  6. Java多线程学习三十八:你知道什么是 CAS 吗
  7. CSS基础——CSS 背景(background)【学习笔记】
  8. 2018百度之星程序设计大赛 - 资格赛 P1006三原色图(MST,并查集)
  9. Asp.Net MVC4 Bundle捆绑压缩技术
  10. 一个小小的发现--音频也八卦
  11. mysql的三种安装方式(详细)
  12. 如何快速的把JSON转Excel怎么转?
  13. android 设内网固定ip,如何给手机设置一个固定的内网ip
  14. hive 求两个月之间的时间间隔(月留存)
  15. createfile调用失败_Java NIO Files.createFile()以NoSuchFileException失败
  16. mysql时间相减得到天数保留两位_[转]Mysql日期函数-日期相减返回天数
  17. 互联网金融平台功能分析及微服务架构设计
  18. 学习同学们的博客经验
  19. 深圳弘辽科技:淘宝扣分要重视,别捡了芝麻丢了西瓜!
  20. 李弘毅机器学习笔记:第九章—Hello world of dee

热门文章

  1. 《iOS 高级编程》之Tableview进阶指南
  2. CSDN文章被洗稿、抄袭严重!用Python做一个“基于搜索引擎的文章查重工具”,解决!...
  3. python ldap3获取所有用户信息_Python使用Ldap3进行Windows AD域管理
  4. Java 多线程快速入门(面试概念解答一)
  5. localPosition和anchoredPosition、offsetMin和offsetMax、SizeDelta
  6. 走路步数怎么在屏幕上显示_华为走步步数不在屏幕上显示如何设置
  7. 计算机毕设——手机天气预报系统
  8. docker-compose(部署微服务+MySQL)
  9. 1 MySQL8绿色版
  10. c#面向对象程序设计——类的抽象