The previous section learned to start another activity through intent. This section learns to pass values by intent.
Still create two activity, I still use the former mainactivity and secondactivity
Look at the mainactivity layout file first, secondactivity layout file is not listed, and the same as the previous lesson:
<textview android:id= "@+id/textview1" android:layout_width= "Wrap_conten T "android:layout_height=" Wrap_content "android:layout_alignparenttop=" true "Android:layout_centerhor Izontal= "true" android:layout_margintop= "53DP" android:text= "mainactivity"/> <button androi D:id= "@+id/button1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android: layout_alignleft= "@+id/textview1" android:layout_below= "@+id/textview1" android:layout_margintop= "188DP" android:text= "button"/> <edittext android:id= "@+id/edittext1" android:layout_width= "Wrap_conte NT "android:layout_height=" Wrap_content "android:layout_below=" @+id/textview1 "Android:layout_centerh Orizontal= "true" android:layout_margintop= "87DP" android:ems= "ten"/>
I added a edittext, by entering the characters in the EditText, and then passing it to the TextView display in secondactivity
Mainactivity the logical part of the code:
public class Mainactivity extends Activity {private EditText EditText; @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Gets the button control button button = (button) Findviewbyid (r.id.button1); Get EditText control editText = (editText) Findviewbyid (R.ID.EDITTEXT1); Sets the listener for the button control button.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//When pressed, Jump to SECONDACTIVITYLOG.I ("Activity_log", "button press") via intent,//new a intentintent intent = new Intent ( Mainactivity.this, Secondactivity.class);//Gets the character entered in EditText string string = Edittext.gettext (). toString ();/ Will get the character put into Intentintent.putextra ("12345", string);//Start activitystartactivity (intent);}});} }
Logic code in Secondactivity:
public class Secondactivity extends Activity {private TextView TextView; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_second); LOG.I ("Activity_log", "secondactivity created"); TextView = (TextView) Findviewbyid (R.ID.TEXTVIEW1);// Get the intent that starts secondactivity, that is, mainactivity in new intentintent intent = this.getintent ();//Get the value passed in the bundle bundle = Intent.getextras ();//the corresponding valuestring string = bundle.getstring ("12345") is found through the key value;//Set Textviewtextview.settext ( string);}}
The final effect:
In this section, the value is passed through the intent, and then the class capacity of the value is displayed, of course, the value is divided into a number of types, I only show the string type. Everyone else can try.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Four components learning activity three