Implementation of Android Auto-login function

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.