a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統 > Android > 正文

Android 幾種屏幕間跳轉的跳轉Intent Bundle

2020-04-11 11:58:27
字體:
來源:轉載
供稿:網友

屏幕使用一個活動來實現,屏幕間是相互獨立的,屏幕之間的跳轉關系通過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");

    }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 99热热热 | 91精品一区二区三区久久久久 | 国产精品一区免费观看 | 叶山小百合av一区二区 | 日本久久精品视频 | 日韩欧美在线一区 | 羞羞视频在线观看免费 | 在线看h| 久久激 | 久久久www| 日韩欧美三级在线 | 本道综合精品 | 久久小视频 | 成人免费视频一区二区 | 久久精彩视频 | 国产美女高潮一区二区三区 | 99精品国产在热久久 | 色婷婷成人网 | 亚洲视频免费在线 | 九九热精 | 国产午夜精品一区二区三区视频 | 久久久久久久久免费视频 | 国产欧美精品一区二区色综合朱莉 | 免费观看一级特黄欧美大片 | 欧美一区二区三区精品 | av大帝| 亚洲天天草| 欧美午夜一区二区三区免费大片 | 国产精品亚洲一区 | 国产精品久久九九 | 日本在线视频观看 | 在线免费国产 | 免费一级欧美在线观看视频 | 午夜免费| 欧美黄色一级毛片 | 色综合五月婷婷 | 免费看国产一级特黄aaaa大片 | 日韩免费网站 | 午夜影院在线观看 | 亚洲精品一区二区 | 亚洲三区视频 |