Jumping from one screen to another, one activity jumps to another activity,intent class for passing data and jumps between components, including not only activity.
Packagecom.sxt.day04_01;Importcom.sxt.day04_01.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 () {Setexitclicklistener ();//set the listener for the exit buttonSetloginclicklistener ();//Set the login button listener } //Set the login button listener 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 ; } Intent Intent=NewIntent (loginactivity. This, Mainactivity.class); //reduces the coupling of 2 activity (the degree of tightness), the relationship between 2 classes in Java is less, modifying one class has little impact on another class ,Intent.putextra ("id", id);//The health must be a string, and the value can be any typeIntent.putextra ("Password", PWD); User User=NewUser (Integer.parseint (ID), PWD); Intent.putextra ("User", user);//user to implement Serializable interfacestartactivity (Intent); } }); } //set the listener for the exit button Private voidSetexitclicklistener () {Findviewbyid (r.id.btnexit). Setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {finish (); } }); } Private voidInitview () {Metid= (EditText) Findviewbyid (R.id.etid);//New is out of the objectMetpwd=(EditText) Findviewbyid (R.ID.ETPWD); }}
Packagecom.sxt.day04_01;Importcom.sxt.day04_01.entity. User;ImportAndroid.os.Bundle;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.util.Log;ImportAndroid.view.Menu; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Intent Intent=getintent ();//Gets the intent of the last activity passString Id=intent.getstringextra ("id");//gets the data of type string, Getintextra gets the int type dataString Pwd=intent.getstringextra ("Password"); User User= (user) Intent.getserializableextra ("user")); LOG.I ("Main", "id=" +id+ ", Password:" +pwd); LOG.I ("Main", user.tostring ()); }}
Android Activity jump