Main_layout.xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"> <EditTextAndroid:layout_width= "300DP"Android:layout_height= "Wrap_content"Android:id= "@+id/myet"android:textsize= "40DP" /> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "normal way to the second page"android:textsize= "30DP"Android:onclick= "onclick" /> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Return to second page"android:textsize= "30DP"Android:onclick= "Onclick3" /></LinearLayout>
Mainactivity.java
Packagecom.example.chenshuai.test321;Importandroid.content.Intent;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast;/*** Created by Chenshuai on 2016/3/21.*/ Public classMainactivityextendsappcompatactivity{@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.main_layout); } //Normal Way Public voidonclick (view view) {LOG.E ("Tag", "Click on button to listen for trigger"); //Static Methods//do not instantiate, call directly with the class name//Build a Toast//method ChainToast.maketext ( This, "Click on button to listen for trigger", Toast.length_long). Show (); //Toast.maketext (This, "button click-to-listen triggered", Toast.length_short). Show (); /*Toast ta = Toast.maketext (This, "button click-to-listen triggered", toast.length_long); Ta.show ();*/ //get the information you want to pass//Get View instanceEditText Myet =(EditText) Findviewbyid (R.id.myet); String Str=Myet.gettext (). toString (); Intent Intent=NewIntent ( This, Activity2.class); //Store Content//The Extra extension is actually a hashmap, and the limit PutExtra is a bundleIntent.putextra ("Myet", str); //start, no return value requiredstartactivity (Intent); } //the way with the return value Public voidOnclick3 (view view) {EditText Myet=(EditText) Findviewbyid (R.id.myet); String Str=Myet.gettext (). toString (); Intent Intent=NewIntent ( This, Activity2.class); //Store Content//The Extra extension is actually a hashmap, and the limit PutExtra is a bundleIntent.putextra ("Myet", str); //starting mode, with return value//The first parameter of intent//second parameter Requestcode request codeStartactivityforresult (intent,1); } //handling the Listener (callback method) for returning information//listening for all returned information//there must be requestcode to distinguish which request is returned by@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) { Super. Onactivityresult (Requestcode, ResultCode, data); if(Requestcode = = 1) { if(ResultCode = =RESULT_OK) { //Get return informationString str = Data.getextras (). getString ("MyTV"); //ShowEditText Myett =(EditText) Findviewbyid (R.id.myet); Myett.settext (str); //Toast.maketext (This, "Return information success", Toast.length_long); } Else{Toast.maketext ( This, "There is a problem returning information", Toast.length_long); } } }}
Activity_2.xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns: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"Tools:context= "Com.example.chenshuai.test321.Activity2"> <EditTextAndroid:layout_width= "200DP"Android:layout_height= "Wrap_content"android:textsize= "40DP"Android:id= "@+id/mytv" /> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "normal return"android:textsize= "40DP"Android:onclick= "Onclick1" /> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Return with return value"android:textsize= "40DP"Android:onclick= "Onclick2" /></LinearLayout>
Activity2.java
Packagecom.example.chenshuai.test321;Importandroid.content.Intent;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.view.View;ImportAndroid.widget.EditText; Public classActivity2extendsappcompatactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_2); //Receive Information//Get Intent//pass over the intentIntent in =getintent (); String s= In.getextras (). getString ("Myet"); EditText MyTV=(EditText) Findviewbyid (R.ID.MYTV); Mytv.settext (s); } //Normal return Public voidonclick1 (view view) {//Close Current Activityfinish (); } Public voidOnclick2 (view view) {//store return data with intentEditText MyTV=(EditText) Findviewbyid (R.ID.MYTV); Intent in=NewIntent (); //set the return data//set the ResultCode first, then set the intent to store the dataSetresult (Result_ok,in.putextra ("MyTV", Mytv.gettext (). toString ())); Finish (); }}
Androidminifest.xml
Package Com.example.chenshuai.test321;import Android.content.intent;import Android.os.bundle;import Android.support.v7.app.appcompatactivity;import Android.view.view;import Android.widget.edittext;public Class Activity2 extends Appcompatactivity {@Override protected void onCreate (Bundle savedinstancestate) {Super.on Create (savedinstancestate); Setcontentview (r.layout.activity_2); Receive information//get intent//pass over Intent Intent in = Getintent (); String s = In.getextras (). getString ("Myet"); EditText MyTV = (EditText) Findviewbyid (R.ID.MYTV); Mytv.settext (s); }//Normal return public void onclick1 (view view) {///close Current Activity finish (); The public void Onclick2 (view view) {///store return data is also used intent EditText MyTV = (EditText) Findviewbyid (r.id.m YTV); Intent in = new Intent (); Set the return data//Set ResultCode First, then set the intent to store the data Setresult (Result_ok,in.putextra ("MyTV", Mytv.gettext (). toString ())); Finish (); }}
android--Return of activity jumps (no return value and return value)--Total (Test)