Mainactivity 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 main_button1 = null; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);// Initializes the control in the layout file to the logical control file Findview ();//Declaration Intent (Plan), from which acivity to which activityfinal Intent Intent = new Intent (This, Testactivity.class);//Store simple parameter Intent.putextra ("username", "I am little Android") to intent,//Store Bundle object Bundle in intent bundle = new Bundle (); Bundle.putstring ("Password", "123456"); Intent.putextras (bundle);//Take an anonymous function to set a click event for a button main_ Button1.setonclicklistener (New Onclicklistener () {public void OnClick (view view) {// Go to the next activityMainActivity.this.startActivity (intent);}});} /** * Activity created when initializing control */private void Findview () {main_button1 = (Button) Findviewbyid (R.id.main_button1);}}
The mainactivity layout is as follows:
<relativelayout 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:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin " android:paddingright=" @dimen/activity_horizontal_margin " android:paddingtop=" @dimen /activity_vertical_margin " tools:context=". Mainactivity "> <button android:id=" @+id/main_button1 " android:layout_width=" Wrap_content " android:layout_height= "wrap_content" android:layout_margintop= "38DP" android:text= " Pass data to Testactivty "/></relativelayout>
Testactivity as follows:
Package Com.example.helloworld;import Android.app.activity;import Android.content.intent;import android.os.Bundle; Import Android.widget.textview;public class Testactivity extends activity{private TextView tv1_test = null;private TextView tv2_test = null, @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.test); Findview ();//Get intentintent intent = getintent () of the previous activity transfer; String username = Intent.getstringextra ("username");//Get the previous activity encapsulated in intent bundlebundle bundle = Intent.getextras (); String password = (string) bundle.get ("password"); Tv1_test.settext (username); tv2_test.settext (password);} /** * Activity created when initializing control */private void Findview () {tv1_test = (TextView) Findviewbyid (R.ID.TEST_TV1); tv2_test = (TextView) Findviewbyid (R.ID.TEST_TV2);}}
The testactivity layout is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" android:background= "@android: Color/white" > <textview android:id= "@+id/ TEST_TV1 " android:layout_width=" match_parent " android:layout_height=" wrap_content "/> < TextView android:id= "@+id/test_tv2" android:layout_width= "match_parent" android:layout_height= " Wrap_content "/></linearlayout>
Do not forget to configure activity in the Androidmanifest.xml file
<activity android:name= "com.example.helloworld.MyMainActivity" android:label= "@string/title_ Activity_my_main "> </activity> <activity android:name=" Com.example.helloworld.TestActivity "> </activity>