1, create a new project;
2. Add a button to the activity and change the button's name and ID.
3. Create another activity, add a TextView, and set the ID. Such as:
4. Implement the following code in Mainactivity:
Public classMainactivityextendsappcompatactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //Create and implement a listenerFindviewbyid (R.id.button1). Setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {//What the listener is going to doIntent i=NewIntent (mainactivity. This, Main2activity.class);//mainactivity and main2ativity are connected to allow data to pass between them.I.putextra ("Data", "Nihao");//use Putextra to transfer content to another activity, whose name is data, and the value is NihaoStartActivity (i);//start a second activity and pass I to the past } }); }}
5. Implement the following code in Mian2activity:
Public classMain2activityextendsappcompatactivity {PrivateTextView TV; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main2); Intent I=getintent ();//because Mian2activity is initiated by intend, the data associated with this activity is obtained by Getintend.tv=(TextView) Findviewbyid (R.ID.TV22); Tv.settext (I.getstringextra ("Data"));//because the name is data when passed through Putextra in mainactivity, the value is Nihao }}
6, through the virtual machine can see the following effect:
In the first activity there is a button,
Click the button on the first one, the other activity starts, and the "Nihao" of the transfer appears
How Android studio delivers simple data between activity