Android Four components Learning Activity IV

Source: Internet
Author: User


The previous section learned to use intent to transmit values. This section learns to get its return value by activating a new activity.

The layout file for Mainactivity is as follows:

    <textview        android:id= "@+id/textview1"        android:layout_width= "wrap_content"        android:layout_height = "Wrap_content"        android:layout_alignparenttop= "true"        android:layout_centerhorizontal= "true"        android:layout_margintop= "53DP"        android:text= "mainactivity"/>    <button        android: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_content"        android:layout_height= "wrap_content"        android:layout_below= "@+id/textview1"        android:layout_centerhorizontal= "true"        android:layout_margintop= "87DP"        android:ems= "ten"/>

The layout file for Secondactivity is as follows: Note: The layout file I dragged up.

<button        android:id= "@+id/button1"        android:layout_width= "wrap_content"        android:layout_height= " Wrap_content "        android:layout_centerhorizontal=" true "        android:layout_centervertical=" true "        Android: text= "Back"/>    <edittext        android:id= "@+id/edittext1        " android:layout_width= "Wrap_content" android:layout_height= "Wrap_content"        android:layout_below= "@+id/textview1"        android:layout_ Centerhorizontal= "true"        android:layout_margintop= "17DP"        android:ems= "ten" >        <requestfocus/ >    </EditText>    <textview        android:id= "@+id/textview1"        android:layout_width= "wrap _content "        android:layout_height=" wrap_content "        android:layout_alignparenttop=" true "        Android: Layout_centerhorizontal= "true"        android:layout_margintop= "83DP"        android:text= "secondactivity"/>

The logical part of the mainactivity:

public class Mainactivity extends Activity {private EditText edittext;private TextView TextView;        @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);        TextView = (TextView) Findviewbyid (R.ID.TEXTVIEW1); Sets the listener for the button control Button.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {///when pressed, through I Ntent jumps to SECONDACTIVITYLOG.I ("Activity_log", "button Press"),//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 activity with return Startactivityforresult (intent, 5678);}); }//When Secondactivity returns, onactivityresult@overrideprotected void is called OnactiVityresult (int requestcode, int resultcode, Intent data) {//TODO auto-generated method stubif (Requestcode = = 5678)//Judgment Request code {if (ResultCode = = 8890)//Judgment return code {//Take out returned data displayed on TextView bundle b= Data.getextras (); String string = B.getstring ("12345"); Textview.settext (string);}} Super.onactivityresult (Requestcode, ResultCode, data);}}
Logic code for Secondactivity:

public class Secondactivity extends Activity {private TextView textview;private EditText edittext;private Button Backbutt On;private Intent Intent; @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); editText = (EditText) Findviewbyid (R.ID.EDITTEXT1); Backbutton = (Button) Findviewbyid (r.id.button1);//Get secondactivity to start intent, That is, the new intentintent = This.getintent () in mainactivity;//Get the value passed in bundle bundle = Intent.getextras ();// The corresponding valuestring string = bundle.getstring ("12345") is found through the key value;//Textviewtextview.settext set (string); Backbutton.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {///Intent data string passed in backstring = Edittext.gettext (). toString (), Intent.putextra ("12345", backstring);//Set Return code Setresult (8890, intent);// Turn off the current activityfinish ();}});}}

The display results are as follows:

Return to Mainactivity after the appearance of





Ok. The value is passed through the intent and the new activity return value is finished.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Four components Learning Activity IV

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.