Android Remember password and auto login

Source: Internet
Author: User

Import android.app.Activity;
Import android.content.Intent;
Import android.content.SharedPreferences;
Import Android.content.SharedPreferences.Editor;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.CheckBox;
Import Android.widget.EditText;

public class Denglu extends Activity implements onclicklistener{
private EditText name;
private EditText password;
Private CheckBox box1;
Private CheckBox Box2;
Private Sharedpreferences sharedpreferences;
Private button button;

@Override
protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.denglu);
Button = (button) Findviewbyid (R.id.button);
Name = (EditText) Findviewbyid (r.id.name);
Password = (EditText) Findviewbyid (R.id.password);
Box1 = (CheckBox) Findviewbyid (R.id.baocun);
Box2 = (CheckBox) Findviewbyid (R.id.zidong);
Sharedpreferences = getsharedpreferences ("Zhuce", 0);
Remember the password
Boolean IsCheckBox1 =sharedpreferences.getboolean ("CheckBox1", false);
Automatic Login
Boolean IsCheckBox2 =sharedpreferences.getboolean ("CheckBox2", false);



String names=sharedpreferences.getstring ("name", "");
String passwords=sharedpreferences.getstring ("Password", "");

if (isCheckBox1) {
if (!names.equals ("") &&!passwords.equals ("")) {
Name.settext (names);
Password.settext (Passwords);
Box1.setchecked (TRUE);
}else{
if (!names.equals ("")) {
Name.settext (names);
}
}

}
if (IsCheckBox2) {

Intent Intent = new Intent (denglu.this, Zhuye.class);
StartActivity (Intent);

}
Button.setonclicklistener (this);


}

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Switch (V.getid ()) {
Case R.id.button:
if (box1.ischecked ()) {
Sharedpreferences.edit (). Putboolean ("CheckBox1", true). commit ();
Sharedpreferences.edit (). putstring ("name", Name.gettext (). toString ()). commit ();
Sharedpreferences.edit (). putstring ("Password", Password.gettext (). toString ()). commit ();

}else{
Sharedpreferences.edit (). Putboolean ("CheckBox1", false). commit ();
Sharedpreferences.edit (). putstring ("name", Name.gettext (). toString ()). commit ();
Sharedpreferences.edit (). putstring ("Password", ""). commit ();
}
if (box2.ischecked ()) {
Sharedpreferences.edit (). Putboolean ("CheckBox2", true). commit ();
}

Intent Intent = new Intent (denglu.this, Zhuye.class);
StartActivity (Intent);
Break

Default
Break
}
}
}

Android Remember Password and automatic login

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.