Android: Use Sharedpreferences to implement automatic login _android

Source: Internet
Author: User
Tags gettext stub

This article describes Android: using Sharedpreferences to automate login, as follows:

Main code:

public class Loginactivity extends activity {private edittext username;
 Private EditText UserPassword;
 Private CheckBox remember;
 Private CheckBox Autologin;
 Private Button login;
 Private Sharedpreferences sp;

 
 Private String Usernamevalue,passwordvalue; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated Method stub super.oncreate (save
  Dinstancestate);

  
  Setcontentview (R.layout.login);
  Initialize user name, password, remember password, auto login, login button username = (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 choose to remember the password, then enter the login page also automatically tick remember the password, and fill in the username 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 automatically check the automatic login if (choseautologin) {autologin.setchecked (true); Login.setonclicklistener (New Onclicklistener () {///Default login account tinyphp, password 123 @Override public void on
    Click (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 username and password editor.putstring ("user_name", usernamevalue);
     
     Editor.putstring ("PASSWORD", Passwordvalue);      
   Remember Password if (remember.ischecked ()) {   Editor.putboolean ("Remember", true);    
     }else{Editor.putboolean ("Remember", false);        
      //Whether to automatically log in if (autologin.ischecked ()) {Editor.putboolean ("Autologin", true);
      }else{Editor.putboolean ("Autologin", false);
      
     } editor.commit ();
     Jump Intent Intent =new Intent (loginactivity.this,successactivity.class);
    StartActivity (Intent);
    else {toast.maketext (loginactivity.this, "username or password is wrong, please login again!", Toast.length_short). Show ();

 }

   }

  });
 }

}


<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/"
 Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical "
  android:padding= "10DP" > <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "User name:"/> <edittext android:id= "@+id/username" android:layout_width= "Match_parent" Android:layo
  ut_height= "Wrap_content" android:ems= "ten" android:inputtype= "Textpersonname" > </EditText> <textview Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_margintop= "10DP" Android: text= "Password:"/> <edittext android:id= "@+id/userpassword" android:layout_width= "Match_parent" android:layout_he ight= "Wrap_content" android:ems= "ten" android:inputtype= "Textpassword" > </EditText> <checkbox Android : id= "@+id/remember" Android:layout_widtH= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Remember Password"/> <checkbox "android:id=" Login "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:text=" Automatic login "/> <Butt On android:id= "@+id/login" android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:text= "
 Login "/> </LinearLayout>

SOURCE Download: Source code

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.