In the development of Android, we do the user login needs to save the user name and password, the next time you open remember the username and password, that is, remember the user name and password. There are several ways to remember how to save your user name and password, and here's how to save sharaedpreferenses. This method of saving is mainly in XML format to save the file, in the development of the explorer can be seen. Here is a small example, mainly code, want to help developers: the main code to implement: Private EditText username,password;private static final String file_name= " Saveusernamepwd "; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); user_name = (EditText) This.findviewbyid (r.id.username);p WD = (EditText) This.findviewbyid (R.id.password); Sharedpreferences sharedpreferences = getsharedpreferences (file_name, context.mode_private);//Get saved data from a file string Usernamecontent = sharedpreferences.getstring ("username", "" "); String passwordcontent = sharedpreferences.getstring ("Password", "" ");//Determine if there is data present and handle it accordingly if (usernamecontent! = null && "". Equals (usernamecontent)) User_name.settext (usernamecontent); if (passwordcontent! = null &&! "). Equals (passwordcontent)) Pwd.settext (passwordcontent);} @Overrideprotected void OnsavecontENT () {super.onstop (); String usernamecontent = Username.gettext (). toString (); String passwordcontent = Password.gettext (). toString ();//Get sharedpreferences when you need to set two parameters//first is the name of the saved file, The second parameter is the saved mode (whether used only by the application) sharedpreferences sharedpreferences =getsharedpreferences (file_name, context.mode_private); Editor editor = Sharedpreferences.edit ();//Add the data to be saved Editor.putstring ("username", usernamecontent); Editor.putstring ( "Password", passwordcontent);//Confirm Save Editor.commit ();} This is relatively simple, mainly the application of sharaedpreferences. In the OnCreate code is to get the saved data, and display in the User name password box, Onsavecontent is mainly to remember the user name and password, that is, to save the user name password information to the relevant files, for these two reasonable use, you can realize to remember the user name and password.
================
almost all applications involve a login interface, so there is a corresponding remember password and automatic login options. I mainly use the sharedpreference to achieve, the code is relatively simple. If there is something wrong, also want to correct, first post O (∩_∩) o! <ignore_js_op>Longin.rar (162.19 KB, download number: 964)
Here is the code for Main.xml:
<?xml version= "1.0" encoding= "Utf-8"?>
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:background= "@drawable/chat_bg_default"
>
<textview
android:layout_height= "50DP"
Android:layout_width= "Fill_parent"
android:gravity= "Center"
Android:textsize= "24SP"
android:text= "Login Screen"
Android:textcolor= "@color/white"
android:background= "@drawable/song_index_item_bg"/>
<imageview
Android:id= "@+id/iv"
android:layout_height= "220DP"
Android:layout_width= "230DP"
android:layout_margintop= "110DP"
android:src= "@drawable/background"
Android:layout_centerhorizontal= "true"
Android:scaletype= "Fitxy"/>
<textview
Android:id= "@+id/tv_name"
android:layout_height= "Wrap_content"
Android:layout_width= "Wrap_content"
Android:textsize= "15SP"
android:layout_margintop= "7DP"
android:text= "Account Number:"
android:layout_alignleft= "@+id/iv"
android:layout_aligntop= "@+id/iv"
android:layout_marginleft= "7DP"
Android:textcolor= "@color/green"/>
<edittext
Android:id= "@+id/et_name"
android:layout_height= "25DP"
Android:layout_width= "210DP"
android:layout_margintop= "7DP"
android:layout_below= "@+id/tv_name"
android:background= "@drawable/shape"
android:layout_alignleft= "@+id/tv_name"/>
<textview
Android:id= "@+id/tv_pass"
android:layout_height= "Wrap_content"
Android:layout_width= "Wrap_content"
Android:textsize= "15SP"
android:text= "Password:"
android:layout_alignleft= "@+id/tv_name"
android:layout_below= "@+id/et_name"
android:layout_margintop= "7DP"
Android:textcolor= "@color/green"/>
<edittext
Android:id= "@+id/et_pass"
android:layout_height= "25DP"
Android:layout_width= "210DP"
android:background= "@drawable/shape"
android:layout_margintop= "7DP"
android:layout_below= "@+id/tv_pass"
android:layout_alignleft= "@+id/tv_name"/>
<checkbox
Android:id= "@+id/isremenber"
android:layout_height= "Wrap_content"
Android:layout_width= "Wrap_content"
android:layout_alignleft= "@+id/tv_name"
android:layout_margintop= "10DP"
android:text= "Remember Password"
Android:textsize= "11SP"
Android:textcolor= "@color/huise"
android:layout_below= "@+id/et_pass"
android:button= "@drawable/mycheckbox"/>
<checkbox
Android:id= "@+id/isloginself"
android:layout_height= "Wrap_content"
Android:layout_width= "Wrap_content"
android:layout_alignright= "@+id/et_name"
android:layout_margintop= "10DP"
android:text= "Automatic Login"
Android:textsize= "11SP"
Android:textcolor= "@color/huise"
android:layout_below= "@+id/et_pass"
android:button= "@drawable/mycheckbox"/>
<button
Android:id= "@+id/longin"
android:layout_height= "25DP"
Android:layout_width= "210DP"
android:layout_below= "@+id/isloginself"
android:layout_alignleft= "@+id/et_pass"
android:layout_margintop= "10DP"
android:text= "Login"
android:gravity= "Center"
android:background= "@drawable/loginbtn"/>
</RelativeLayout>
The main activity is also relatively straightforward directly on the code:
public class Longinactivity extends Activity {
private EditText name;
Private EditText Pass;
Private CheckBox Isremenber;
Private CheckBox isloginself;
Private Button Longin;
Private ProgressDialog Mdialog;
/** called when the activity is first created. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Name= (EditText) Findviewbyid (r.id.et_name);
Pass= (EditText) Findviewbyid (R.id.et_pass);
Isremenber= (CheckBox) Findviewbyid (R.id.isremenber);
isloginself= (CheckBox) Findviewbyid (r.id.isloginself);
Longin= (Button) Findviewbyid (R.id.longin);
Final sharedpreferences pre=getsharedpreferences ("Longinvalue", mode_world_writeable);
if (pre!=null) {
Remember the password
if (Pre.getboolean ("ISRMB", false) ==true) {
Name.settext (pre.getstring ("name", null));
Pass.settext (pre.getstring ("pass", null));
Isremenber.setchecked (TRUE);
}
if (Pre.getboolean ("Islgs", false) ==true) {
Isloginself.setchecked (TRUE);
Creatdialog ();
New Thread () {
public void Run () {
try {
Thread.Sleep (3000);
if (mdialog.isshowing ()) {
Mdialog.dismiss ();
}
Intent intent2=new Intent (longinactivity.this,show.class);
StartActivity (Intent2);
} catch (Exception e) {
Todo:handle exception
}
}
}.start ();
}
}
Isremenber.setoncheckedchangelistener (New Oncheckedchangelistener () {
@Override
public void OnCheckedChanged (Compoundbutton buttonview, Boolean isChecked) {
TODO auto-generated Method Stub
if (isremenber.ischecked () ==false) {
Isloginself.setchecked (FALSE);
}
}
});
Isloginself.setoncheckedchangelistener (New Oncheckedchangelistener () {
@Override
public void OnCheckedChanged (Compoundbutton buttonview, Boolean isChecked) {
TODO auto-generated Method Stub
Isremenber.setchecked (TRUE);
}
});
Longin.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
if (!name.gettext (). ToString (). Equals ("") &&!pass.gettext (). ToString (). Equals ("")) {
if (isloginself.ischecked ()) {
Pre.edit (). Putboolean ("ISRMB", True). Putboolean ("Islgs", True). Putstring ("name", Name.gettext (). toString ())
. putstring ("Pass", Pass.gettext (). toString ()). commit ();
}else{
if (isremenber.ischecked ()) {
Pre.edit (). Putboolean ("ISRMB", True). Putboolean ("Islgs", false). Putstring ("name", Name.gettext (). toString ())
. putstring ("Pass", Pass.gettext (). toString ()). commit ();
}else{
Pre.edit (). Putboolean ("ISRMB", false). Putboolean ("Islgs", false). Putstring ("name", Name.gettext (). toString ())
. putstring ("Pass", Pass.gettext (). toString ()). commit ();
}
}
Intent intent=new Intent (longinactivity.this,show.class);
StartActivity (Intent);
}else{
Toast.maketext (Getapplicationcontext (), "password or account cannot be empty!" ", Toast.length_long). Show ();
}
}
});
}
private void Creatdialog () {
TODO auto-generated Method Stub
Mdialog=new ProgressDialog (this);
Mdialog.settitle ("in Verification");
Mdialog.setmessage ("Landing please later");
Mdialog.setindeterminate (TRUE);
Mdialog.setcancelable (TRUE);
Mdialog.show ();
}
}<ignore_js_op>
Click Login to enter the second activity, and finally use sharedpreference need to set permissions <uses-permission android:name= "Android.permission.WRITE _external_storage "></uses-permission> so much, I hope we can design a beautiful login interface!
Android Remember Password