A summary of the steps to create an activity jump in Android _android

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.