First, create the project
File->new->android application fill in the application name, project name, package name in sequence
Find Src/com.example.helloworld Mainactivity.java under the engineering catalogue
Second, add code
Copy Code code as follows:
Package Com.example.helloworld;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
public class Mainactivity extends activity
{
Private button button;
@Override
protected void OnCreate (Bundle savedinstancestate)
{
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
This.button = (Button) This.findviewbyid (R.id.button1);
This.button.setOnClickListener (New Onclicklistener () {
@Override
public void OnClick (View v) {
Intent Intent = new Intent ();
Intent.setclass (Mainactivity.this, Intentactivity.class);
Intent.putextra ("name", "Hello World");
StartActivity (Intent);
}
});
}
}
Before adding code, drag a button control up in the Res/layout/activity_main.xml. The button's information is automatically generated in the Activity_main.xml and the ID value is used, which is useful if you use this button
Third, add jump activity
1, right click Src/com.example.helloworld,new->class, fill in the name of Intentactivity, do not add Java can
2, add layout. Right-click at res/layout, new->android XML file, drag a textview up on this XML. The TextView address is automatically added to the R.java at this time
3, add the following code in the Androidmanifest.xml: Completing the registration of the activity
Copy Code code as follows:
<activity
Android:name= "Com.example.helloworld.IntentActivity" > </activity>
III. realization of Intentactivity.java
Copy Code code as follows:
Package Com.example.helloworld;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.TextView;
Import android.content.Intent;
public class Intentactivity extends activity
{
@Override
protected void OnCreate (Bundle savedinstancestate)
{
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_intent);
Intent Intent = Getintent ();
String string = Intent.getstringextra ("name");
TextView TextView = new TextView (this);
Textview.settextsize (40);
Textview.settext (string);
Setcontentview (TextView);
}
}
Four, the operation
Turn on the simulator or cell phone to run.