Androidmanifest.xml
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.hanqi.test4"> <ApplicationAndroid:allowbackup= "true"Android:icon= "@mipmap/ic_launcher"Android:label= "@string/app_name"Android:supportsrtl= "true"Android:theme= "@style/apptheme"> <ActivityAndroid:name=". Mainactivity " > <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter> </Activity> <ActivityAndroid:name=". Main2activity "></Activity> </Application></Manifest>
Mainactivity
PackageCom.hanqi.test4;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 Administrator on 2016/3/21.*/ Public classMainactivityextendsappcompatactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.main_layout); } //Normal Way Public voidONCLICK (View v) {LOG.E ("T4tag", "click on button to monitor is triggered"); //Static Methods//can be called directly with the class name, no instantiation required//Build a Toast instance//Method EvenToast.maketext ( This, "button click-to-listen is triggered", Toast.length_long). Show ();//Toast toast= Toast.maketext (This, "button click-Listen is triggered", toast.length_long);//toast.show (); //with Intent//get the information you want to pass//Get View instanceEditText myet=(EditText) Findviewbyid (R.id.myet); String String=Myet.gettext (). toString (); Intent Intent=NewIntent ( This, Main2activity.class); //Store Content//The Getextra Bundle is actually a hashmap limit//Intent.getextras (). putstring ("Myet", string);Intent.putextra ("Myet", String); StartActivity (Intent); } //with the return way Public voidOnCLICK (View v) {EditText Myet=(EditText) Findviewbyid (R.id.myet); String String=Myet.gettext (). toString (); Intent Intent=NewIntent ( This, Main2activity.class); //Store Content//The Getextra Bundle is actually a hashmap limit//Intent.getextras (). putstring ("Myet", string);Intent.putextra ("Myet", String); //How to start a return data//The first parameter of intent//second parameter Requestcode request codeStartactivityforresult (Intent, 1); } //override the listener that handles return information (callback method)//Onactivityresult Universal Monitor listens for all return information//there must be requestcode to distinguish which request is returned.@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) { Super. Onactivityresult (Requestcode, ResultCode, data); LOG.E ("TAG", "requestcode=" +requestcode+ "ResultCode" +ResultCode); if(Requestcode ==1 ) { if(ResultCode = =RESULT_OK) { //Get return informationString string = Data.getextras (). getString ("MyTV"); EditText EditText=(EditText) Findviewbyid (R.id.myet); Edittext.settext (string); Toast.maketext ( This, "return info =" +string, Toast.length_long); } Else{Toast.maketext ( This, "There is a problem returning information", Toast.length_short); } } }}
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= "100DP"Android:layout_height= "Wrap_content"Android:id= "@+id/myet" /> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Normal Way"Android:onclick= "ONCLICK" /> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "with return mode"Android:onclick= "OnCLICK" /></LinearLayout>
Main2activity
PackageCom.hanqi.test4;Importandroid.content.Intent;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.view.View;ImportAndroid.widget.EditText; Public classMain2activityextendsappcompatactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main2); //Receive Information//Get Intent//pass over the intentIntent intent=getintent (); String s= Intent.getextras (). getString ("Myet"); EditText MyTV=(EditText) Findviewbyid (R.ID.MYTV); Mytv.settext (s); } //Normal return Public voidonclick (View V) {//Close Current Activityfinish (); } Public voidOnclock (View v) {//store return data with intentEditText mytv=(EditText) Findviewbyid (R.ID.MYTV); Bundle Bundle=NewBundle (); Bundle.putstring ("MyTV", Mytv.gettext (). toString ()); //set the return data//set the Reaultcode first, then set the intent to store the dataSetresult (RESULT_OK,NewIntent (). PutExtra ("MyTV", Mytv.gettext (). toString ())); //Close Current Activityfinish (); }}
Activity_main2.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.hanqi.test4.Main2Activity"> <EditTextAndroid:layout_width= "100DP"Android:layout_height= "Wrap_content"Android:text= "Test"Android:id= "@+id/mytv" /> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "normal return"Android:onclick= "onclick" /> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Return with data"Android:onclick= "Onclock" /></LinearLayout>
Android--activity a jump with a return value