We used intent to jump between activities, in fact intent can also pass the data when the activity is started.
Intent provides a series of Putextra methods so that we can temporarily present the data that we want to pass in intent, and when another activity is started, we just need to remove the data from intent.
Through examples we look at:
1) New Loginapp Project
2) Longinactivity.java
Importandroid.content.Intent;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.EditText; Public classLoginactivityextendsappcompatactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.login_activity_layout); Button Button=(Button) Findviewbyid (R.id.button); Button.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {EditText text=(EditText) Findviewbyid (r.id.username); String username=Text.gettext (). toString (); //start another activity with display intentIntent Intent =NewIntent (loginactivity. This, Welcomeactivity.class); //Use the Putextra (Kay,value) method to encapsulate what you want to pass to the next activityIntent.putextra ("username", username); //Start Activitystartactivity (Intent); } }); }}
3) Welcomeactivity.java
Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.widget.TextView;/*** Created by LGB on 2015/8/7.*/ Public classWelcomeactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.welcome_activity_layout); //get the intent to start the activityIntent Intent =getintent (); //get the data encapsulated in the boot intentString username = Intent.getstringextra ("username"); TextView text=(TextView) Findviewbyid (r.id.username); //set the text in TextView to usernameText.settext (username.tostring ()); }}
4) Login_activity_layout.xml
<Relativelayoutxmlns: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"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Android:paddingbottom= "@dimen/activity_vertical_margin"Tools:context=". Longinactivity "> <EditTextAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/username"Android:layout_alignparenttop= "true"Android:layout_alignparentstart= "true"Android:layout_margintop= "32DP"Android:layout_alignparentend= "true" /> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Login"Android:id= "@+id/button"Android:layout_below= "@+id/username"Android:layout_centerhorizontal= "true" /></Relativelayout>
5) Welcome_activity_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"> <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "User name"Android:id= "@+id/username" /> <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Welcome"Android:id= "@+id/welcome" /></LinearLayout>
6) Androidmanifest.xml
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Cn.lixyz.loginapp" > <ApplicationAndroid:allowbackup= "true"Android:icon= "@mipmap/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" > <ActivityAndroid:name=". Loginactivity "Android:label= "@string/app_name" > <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter> </Activity> <ActivityAndroid:name=". Welcomeactivity "> </Activity> </Application></Manifest>
Run results
Android Notes (iv) data transfer between activity