Landing Page Layout design:
<linearlayout android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:la yout_margintop= "10DP" android:orientation= "horizontal" > <textview android:layout_width= "WR Ap_content "android:layout_height=" wrap_content "android:text=" @string/account "/> <edi Ttext android:id= "@+id/edtaccount" android:layout_width= "150DP" android:layout_height= "WR Ap_content "android:inputtype=" number "android:singleline=" true "/> </LinearLayout> & Lt LinearLayout android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_ margintop= "10DP" android:orientation= "horizontal" > <textview android:layout_width= "Wrap_co Ntent "android:layout_height=" wrap_content "android:text=" @string/password "/> <edittex T android:Id= "@+id/edtpassword" android:layout_width= "150DP" android:layout_height= "Wrap_content" a Ndroid:inputtype= "Textpassword" android:singleline= "true"/> </LinearLayout> <button A Ndroid:id= "@+id/btnlogin" android:layout_width= "200DP" android:layout_height= "Wrap_content" android:l ayout_margintop= "10DP" android:text= "@string/login"/>
Logout Page Layout design:
<textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android: layout_margintop= "10DP" android:text= "@string/Logout page" android:textsize= "15sp"/> <button Android:id= "@+id/btncancel" android:layout_width= "200DP" android:layout_height= "Wrap_content " android:text= "@string/cancel"/>
Loginactivity.java:
Package Com.xiaoyan.autologin;import Android.app.activity;import Android.content.context;import Android.content.intent;import Android.content.sharedpreferences;import Android.content.SharedPreferences.Editor; Import Android.os.bundle;import android.view.view;import android.widget.button;import android.widget.EditText; Import Android.widget.toast;public class Loginactivity extends Activity {//define component private EditText Edtaccount;private EditText edtpassword;private button btnlogin;//used to record account number and password private string straccount = "";p rivate string strpassword = ""; @O verrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.login_main);//Set title Settitle ("Login");//Get Sharedpreferences object sharedpreferences share = Getsharedpreferences ("Login", context.mode_private); straccount = share.getstring ("Account", ""); strpassword = Share.getstring ("Password", "" ");//Determine if it was previously signed in if (share = = null) {init ();} else {//determine if you just signed out if (Share.getboolean ("Loginbool", False) {//Jump to logout page and destroy current activityintent intent = new Intent (Loginactivity.this,cancelactivity.class); StartActivity (Intent) ; Finish ();} else {init ();}}} private void Init () {//Initialize component Edtaccount = (EditText) Findviewbyid (r.id.edtaccount); Edtpassword = (EditText) Findviewbyid (R.id.edtpassword); btnlogin = (Button) Findviewbyid (R.id.btnlogin); Edtaccount.settext (Straccount); Edtpassword.settext (strpassword);//Monitor button Btnlogin.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View arg0) {//Determine if the account and password are entered as null if (Edtaccount.gettext (). toString (). Equals ("") | | Edtpassword.gettext (). toString (). Equals ("")) {Toast.maketext (Loginactivity.this, "account or password cannot be empty", toast.length_short) . Show (); else {//Create Sharedpreferences object to store account number and password, and privatize it sharedpreferences share = getsharedpreferences ("Login", Context.mode_ PRIVATE);//Get editor to store data into sharedpreferences Editor editor = Share.edit (); Editor.putstring ("Account", Edtaccount.gettext (). ToString ()); Editor.putstring ("Password", Edtpassword.gettext (). ToString ()); EditoR.putboolean ("Loginbool", true);//submit data to sharedpreferences Editor.commit ();//Jump to logout page and destroy current activityintent intent = New Intent (Loginactivity.this,cancelactivity.class); startactivity (Intent); Finish ();}});}}
cancelactivity.java:
Package Com.xiaoyan.autologin;import Android.app.activity;import Android.content.context;import Android.content.intent;import Android.content.sharedpreferences;import Android.os.bundle;import Android.view.View Import Android.widget.button;public class Cancelactivity extends Activity {//define component private Button btncancel;@ overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate ( Savedinstancestate); Setcontentview (r.layout.cancel_activity);//Set Caption Settitle ("Cancel");//Initialize page init ();} private void Init () {//Initialize component Btncancel = (Button) Findviewbyid (r.id.btncancel);//Listener Logout button Btncancel.setonclicklistener ( New View.onclicklistener () {@Overridepublic void OnClick (View arg0) {//TODO auto-generated method stub//unregisters the account and destroys the current page shar Edpreferences share = getsharedpreferences ("Login", context.mode_private); Share.edit (). Putboolean ("Loginbool", False). commit (); Intent Intent = new Intent (cancelactivity.this,loginactivity.class); startactivity (Intent); Finish () ;}});}}
Implementation of Android Auto-login function