I. Three ways to pass the parameters
The scene is divided into two steps:
1, from the first activity to pass parameters to the second activity, accept and display.
2, the Second Activity text box to enter information, and upload to the first activity.
You need to define intent before you pass the parameter
Intent i=New Intent (mainactivity. this, theaty. class); // parameter One current activity, parameter two jump activity
The centralized method of transmitting parameters
1, PutExtra
Similar to a key-value pair, the second argument can pass a different type of value
A) Assign value
I.putextra ("Data", "Hellow,zhangsan");
b) Take value
I.getstringextra ("Data")
2. Using bundles
Use different put methods depending on the type
A) Assign value
Bundle b=New bundle (); B.putstring ("name", "Hello, Zhang San");
I.putextras (b);
b) Take the value:
Bundle Data=i.getextras ();d ata.getstring ("name");
3. Using objects
A) Define a user object that contains the name and age properties
Public classUserImplementsparcelable{PrivateString name; Private intAge ; Public intGetage () {return This. Age; } Public voidSetage (intAge ) { This. age=Age ; } PublicString GetName () {return This. Name; } Public voidSetName (String name) { This. name=name; } PublicUser (String name,intAge ) { This. name=name; This. age=Age ; } @Override Public intdescribecontents () {return0; } @Override Public voidWritetoparcel (Parcel dest,inti) {dest.writestring (GetName ()); Dest.writeint (Getage ()); } Public Static FinalCreator<user> creator=NewCreator<user>() {@Override PublicUser Createfromparcel (Parcel source) {return NewUser (source.readstring (), Source.readint ()); } @Override PublicUser[] NewArray (intsize) { return NewUser[size]; } };}
This uses the Android parcelable serial Number interface, overriding the describecontents and Writetoparcel methods, defining the constant creator. Of course, you can also use Java's Serializable interface to achieve less code.
b) Transfer of parameters
I.putextra ("User",new User ("Zhangsan", 18));
c) Take value
User u= (user) I.getparcelableextra ("User"), Tv.settext (String.Format ("user info (name=%s,age=%d)", U. GetName (), U.getage ()));
Second, the implementation of the Code and steps: 1, first define the first activity
Plus button and TextView.
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:id= "@+id/activity_main"android:orientation= "Vertical"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= "Com.example.lisheng.sendargs.MainActivity" > <Button android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" android:id= "@+id/btnstartaty" android:text= "start another activtiy"/> <TextView Android:text=""Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:id= "@+id/textview"/></linearlayout>
2, the first activity after the implementation of the Code platform
Public classMainactivityextendsappcompatactivity {PrivateTextView TextView;//define TextView@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); TextView= (TextView) Findviewbyid (R.id.textview);//get TextView object by IDFindviewbyid (r.id.btnstartaty). Setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {Intent I=NewIntent (mainactivity. This, Theaty.class); I.putextra ("User",NewUser ("Zhangsan", 18)); Startactivityforresult (i,0); } }); } @Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) { Super. Onactivityresult (Requestcode, ResultCode, data); Textview.settext ("Data returned:" +data.getstringextra)); }}
Here's something to be aware of.
1) The acquisition element is based on the element ID Findviewbyid (R.id.textview);
2) To bind the button to a point-click event: setonclicklistener (new View.onclicklistener () {}
3) to get the value returned by the second activity, you need to override the Onactivityresult method
3. Second Activity page
A textview is used to display the value of receiving the first pass, defining a edittext, writing a value, and defining a button;
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:id= "@+id/activity_the_aty"android:orientation= "Vertical"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= "Com.example.lisheng.sendargs.TheAty" > <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Hello world!" Android:id= "@+id/tv"/> <EditText android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:inputtype= "Textpersonname"Android:text= "Name"Android:ems= "10"Android:id= "@+id/edittext"/> <Button Android:text= "Send Back"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:id= "@+id/button"/></linearlayout>
4. Implementation of the second activity background code
Public classTheatyextendsappcompatactivity {PrivateTextView TV; PrivateEditText EditText; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_the_aty); Intent I=getintent (); TV=(TextView) Findviewbyid (r.id.tv); EditText=(EditText) Findviewbyid (R.id.edittext); User u= (user) I.getparcelableextra ("User");//gets the incoming user objectTv.settext (String.Format ("User info (name=%s,age=%d)", U.getname (), U.getage ()));//Show to TextViewFindviewbyid (R.id.button). Setonclicklistener (NewView.onclicklistener () {//Define a button click event@Override Public voidOnClick (view view) {Intent I=NewIntent (); I.putextra ("Data", Edittext.gettext (). toString ()); Setresult (1, i); Finish (); } }); }}
Run, done.
Android Learning Activity Transfer