The screen is implemented by an activity. The screen is independent of each other, and the jump relationship between the screens is implemented by Intent.
Inter-screen jump is divided into the following categories:
1. Screen 1 Jump directly to screen 2
Intent intent = new Intent ();
Intent. setClass (screen 1 activity name. this, screen 2 activity name. class );
StartActivity (intent );
Finish (); // ends the current activity
2. Screen 1 jump to screen 2 with Parameters
Use Bundle to pass parameters.
Example: guessing games
Interface:
Important code:
The computer selection is random. The basic idea of this contact is that the three options are replaced by three numbers, so that the computer can generate a random number, different results are generated based on different numbers.
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, "select", Toast. LENGTH_LONG). show ();
Break;
}
Skip ();
}
// Page Jump
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 );
}
After the jump, you must accept the parameters:
Bundle bundle = this. getIntent (). getBundleExtra ("result ");
Int playerInt = bundle. getInt ("player ");
Int computerInt = bundle. getInt ("computer ");
Complete guessing game code:
Activity_first.xml code
Activity_second.xml code
FirstActivity. java code 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 ("guessing game"); 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, "select", Toast. LENGTH_LONG ). show (); break;} // pass the value to 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 code
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 ("result"); TV = (TextView) findViewById (R. id. show); Bundle bundle = this. getIntent (). getBundleExtra ("result"); int playerInt = bundle. getInt ("player"); int computerInt = bundle. getInt ("computer"); TV. setText ("guessing Result \ n"); TV. append ("your choice:"); intChangeString (playerInt); TV. append ("Computer Selection:"); intChangeString (computerInt); TV. append ("Result:"); if (playerInt = 0) {if (computerInt = 0) {TV. append ("Draw");} else if (computerInt = 1) {TV. append ("you are the winner");} else {TV. append ("computer is the winner") ;}} else if (playerInt = 1) {if (computerInt = 0) {TV. append ("computer is the winner");} else if (computerInt = 1) {TV. append ("Draw");} else {TV. append ("you are the winner") ;}} else {if (computerInt = 0) {TV. append ("you are the winner");} else if (computerInt = 1) {TV. append ("computer is the winner");} else {TV. append ("Draw") ;}} private void intChangeString (int n) {switch (n) {case 0: TV. append ("Stone \ n"); break; case 1: TV. append ("Scissors \ n"); break; case 2: TV. append ("cloth \ n"); break; default: Toast. makeText (secondActivity. this, "error", Toast. LENGTH_LONG ). show (); break ;}}}
3. Jump to screen 2 from Screen 1, return value to screen 1 after execution of screen 2 (with return value jump)
Reference sample program: ReceiveResult (ApiDemo => App => Activity => ReceiveResult)
Important code:
// Adjust Screen 1 to screen 2
Intent intent = new Intent (Forward. this, ForwardTargetActivity. class );
StartActivityForResult (intent, GET_CODE );
// Set the return value on screen 2
SetResult (RESULT_ OK, (new Intent (). setAction ("Violet! "));
Finish ();
// Get the content returned from screen 2 on screen 1
@ 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 ");
}