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

來源:互聯網
上載者:User

螢幕使用一個活動來實現,螢幕間是相互獨立的,螢幕之間的跳轉關係通過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代碼
                                            activity_second.xml代碼
    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()    {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubswitch(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;}//將的到的值傳給secondActivityskip();}        };        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");

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.