Android: Automate login with Sharedpreferences

Source: Internet
Author: User
Tags gettext

Main code:

 Public classLoginactivity extends Activity {PrivateEditText username; PrivateEditText UserPassword; PrivateCheckBox Remember; PrivateCheckBox Autologin; PrivateButton Login; PrivateSharedpreferences sp; PrivateString Usernamevalue,passwordvalue; @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stubsuper.oncreate (savedinstancestate);                Setcontentview (R.layout.login); //Initialize user name, password, remember password, auto login, login buttonUsername =(EditText) Findviewbyid (r.id.username); UserPassword=(EditText) Findviewbyid (R.id.userpassword); Remember=(CheckBox) Findviewbyid (R.id.remember); Autologin=(CheckBox) Findviewbyid (R.id.autologin); Login=(Button) Findviewbyid (R.id.login); SP= Getsharedpreferences ("UserInfo",0); String name=sp.getstring ("user_name",""); String Pass=sp.getstring ("PASSWORD",""); Boolean Choseremember=sp.getboolean ("Remember",false); Boolean Choseautologin=sp.getboolean ("Autologin",false); //Toast.maketext (this, name, Toast.length_short). Show (); //If the last time you chose to remember the password, then enter the login page also automatically tick remember password, and fill in the user name and password        if(Choseremember) {username.settext (name);            Userpassword.settext (pass); Remember.setchecked (true); }        //If the last login selected automatic login, then enter the login page and automatically check the automatic login        if(Choseautologin) {autologin.setchecked (true); } login.setonclicklistener (NewOnclicklistener () {//Default login account tinyphp, password 123@Override Public voidOnClick (View arg0) {Usernamevalue=Username.gettext (). toString (); Passwordvalue=Userpassword.gettext (). toString (); Sharedpreferences.editor Editor=Sp.edit (); //TODO auto-generated Method Stub                if(Usernamevalue.equals ("tinyphp")                        && Passwordvalue.equals ("123") {toast.maketext (loginactivity). This,"Login Successful", Toast.length_short). Show (); //Save user name and passwordEditor.putstring ("user_name", Usernamevalue); Editor.putstring ("PASSWORD", Passwordvalue); //Do you remember the password                    if(remember.ischecked ()) {Editor.putboolean ("Remember",true); }Else{Editor.putboolean ("Remember",false); }                                                                                                    //whether to log on automatically                        if(autologin.ischecked ()) {Editor.putboolean ("Autologin",true); }Else{Editor.putboolean ("Autologin",false);                                            } editor.commit (); //JumpIntent Intent =NewIntent (loginactivity. This, Successactivity.class);                StartActivity (Intent); } Else{toast.maketext (loginactivity). This,"user name or password is wrong, please login again!", Toast.length_short). Show ();    }            }        }); }}
<?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"android:orientation= "vertical"android:padding= "10DP" >    <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "User name:" />    <EditTextAndroid:id= "@+id/username"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:ems= "Ten"Android:inputtype= "Textpersonname" >    </EditText>    <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_margintop= "10DP"Android:text= "Password:" />    <EditTextAndroid:id= "@+id/userpassword"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:ems= "Ten"Android:inputtype= "Textpassword" >    </EditText>    <CheckBoxAndroid:id= "@+id/remember"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Remember Password" />    <CheckBoxAndroid:id= "@+id/autologin"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Automatic Login" />    <ButtonAndroid:id= "@+id/login"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Login" /></LinearLayout>

Download Examples >>>>>>>>>>>

Android: Automate login with Sharedpreferences

Related Article

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.