Main.xml
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"> <ButtonAndroid:id= "@+id/btnlogin"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Login" /> <ButtonAndroid:id= "@+id/btnregister"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Register" /> </LinearLayout>
Login.xml
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" > <TablelayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:stretchcolumns= "1" > <TableRow> <TextViewAndroid:text= "Enter Number" /> <EditTextAndroid:hint= "2-10 characters"Android:id= "@+id/etid"/> </TableRow> <TableRow> <TextViewAndroid:text= "Password" /> <EditTextAndroid:id= "@+id/etpwd"Android:hint= "2-10 characters"Android:password= "true" /> </TableRow> </Tablelayout> <TablelayoutAndroid:layout_margintop= "20DP"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:stretchcolumns= "0,1" > <TableRow> <ButtonAndroid:id= "@+id/btnlogin"Android:background= "@drawable/btn_bg"Android:drawableleft= "@drawable/login32x32"android:padding= "3DP"Android:text= "Login"Android:textcolor= "#fff"android:layout_gravity= "Center_horizontal"/> <ButtonAndroid:id= "@+id/btnexit"Android:background= "@drawable/btn_bg"Android:drawableleft= "@drawable/exit32x32"android:padding= "3DP"Android:text= "Exit"Android:textcolor= "#fff"android:layout_gravity= "Center_horizontal"/> </TableRow> </Tablelayout></LinearLayout>
Mainactivity
Packagecom.sxt.day04_02;Importcom.sxt.day04_02.entity. User;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener; Public classMainactivityextendsActivity {Static Final intAction_login=0; Static Final intaction_register=10; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Setlistener ();//Setting up Listeners } //Setting up Listeners Private voidSetlistener () {Setloginclicklistener (); Setregisterclicklistener (); } Private voidSetregisterclicklistener () {Findviewbyid (r.id.btnregister). Setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {}}); } Private voidSetloginclicklistener () {Findviewbyid (r.id.btnlogin). Setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Intent Intent=NewIntent (mainactivity. This, Loginactivity.class);//Current activity and target activityStartactivityforresult (Intent, action_login);//start loginactivity and ask him to return the result, Action_login request code 0, } }); } @Override//receive the results returned by Acticity protected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {//Requestcode is the request code, that is, here the Action_login,resultcode is the loginactivity return value Ok,data is loginactivity intent object. Super. Onactivityresult (Requestcode, ResultCode, data); if(resultcode!=RESULT_OK) { return ; } Switch(requestcode) { CaseAction_login:user User= (user) Data.getserializableextra ("user")); LOG.I ("Main", user.tostring ()); Break; CaseAction_register: Break; } }}
Loginactivity:
Packagecom.sxt.day04_02;Importcom.sxt.day04_02.entity. User;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;Importandroid.text.TextUtils;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.EditText; Public classLoginactivityextendsActivity {EditText metid,metpwd; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_login); Initview (); Setlistener (); } Private voidSetlistener () {Setloginclicklistener (); Setexitclicklistener (); } Private voidSetexitclicklistener () {//exit does not return the result,Findviewbyid (R.id.btnexit). Setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {finish (); } }); } Private voidSetloginclicklistener () {Findviewbyid (r.id.btnlogin). Setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {String ID=Metid.gettext (). toString (); if(Textutils.isempty (id)) {metid.seterror ("Number cannot be empty"); return ; } String pwd=Metpwd.gettext (). toString (); if(Textutils.isempty (pwd)) {Metpwd.seterror ("Password cannot be empty"); return ; } User User=NewUser (Integer.parseint (ID), PWD); Intent Data=NewIntent (loginactivity. This, Mainactivity.class);//Current activity and returned activityData.putextra ("User", user); Setresult (RESULT_OK, data);//set the return result,Finish ();//Close Current Activity } }); } Private voidInitview () {Metid=(EditText) Findviewbyid (R.id.etid); Metpwd=(EditText) Findviewbyid (R.ID.ETPWD); }}
Android 16 activity with return value