The screen uses an activity to realize, the screen is mutually independent, the jump relation between the screen realizes through the intent.
The screen jumps fall 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 (); End Current Activity
2. Screen 1 with parameters jump to screen 2
Use bundle to pass parameters.
Example: Scissors Game
Interface:
Important Code:
The choice of the computer is random, the basic idea of this contact is, three options use three numbers to replace, let the computer randomly generate a number, according to the different numbers to produce different results.
Copy Code code as follows:
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, "Please 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);
}
To accept parameters after a jump:
Copy Code code as follows:
Bundle Bundle = This.getintent (). Getbundleextra ("result");
int playerint = Bundle.getint ("player");
int computerint = Bundle.getint ("Computer");
Scissors Game Complete Code:
Activity_first.xml Code
Copy Code code as follows:
<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= "Please select the fist you want to give:"
Android:textsize= "20dip"/>
<radiogroup
android:id= "@+id/quans"
android:layout_width= "Wrap_content"
android:layout_height= "wrap_content"
android:o rientation= "Horizontal"
<radiobutton
android:id= "@+id/shitou"
android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:checked= "true"
android:text= "Stone"
android:textsize= "20dip" "/>
<radiobutton
android:id= "@+id/jiandao"
android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:textsize= "20dip"
android:text= "Scissors"/>
<radiobutton
android:id= "@+id/bu"
android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:textsize= "20dip"
android: text= "cloth"/>
</radiogroup>
<button
Android:id= "@+id/chuquan"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:textsize= "20dip"
Android:text= "Out fist"/>
</LinearLayout>
Activity_second.xml Code
Copy Code code as follows:
<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 Code
Copy Code code as follows:
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 ("Scissors Game");
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, "Please select", Toast.length_long). Show ();
Break
}
Pass the value to the 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 Code
Copy Code code as follows:
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 ("scissors result \ n");
Tv.append ("Your choice:");
Intchangestring (Playerint);
Tv.append ("Computer choice:");
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 \ \");
Break
Case 2:
Tv.append ("cloth \ n");
Break
Default
Toast.maketext (secondactivity.this, "error", Toast.length_long). Show ();
Break
}
}
}
3. Screen 1 jump to screen 2, screen 2 after the completion of the return value to screen 1 (with return value jump)
Reference Sample program: Receiveresult (Apidemo => app=>activity=>receiveresult)
Important Code:
Copy Code code as follows:
Screen 1 turn 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 ();
In screen 1 get the content returned from screen 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");
}