当前位置: 移动技术网 > IT编程>移动开发>Android > Android 几种屏幕间跳转的跳转Intent Bundle

Android 几种屏幕间跳转的跳转Intent Bundle

2019年07月24日  | 移动技术网IT编程  | 我要评论

央视论坛,快漫,愚人之城

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

    }

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网