This example for you to share the android with the memory password function of the login interface implementation code for your reference, the specific content as follows
1, design ideas
The main use of sharedpreferences to save user data, this demo is not encrypted, all once the Android system is root, other users can view the user's private directory, the password file is very unsafe. So the real application of the software above, must be encrypted to save, you can choose MD5 encryption.
Sharedpreferences Introduction can refer to this blog post: http://www.jb51.net/article/84859.htm
Textwatcher's introduction can refer to this blog post: http://www.jb51.net/article/84865.htm
2. Function Introduction
The default check "Remember Password" checkbox, click the "Login" button, once the successful landing, save the username and password to the sharedpreferences file.
User name input, through Textwatcher constantly to read the user data, automatically prompts the corresponding "User name", select the user name, will read the Sharedpreferences file, and then automatically complete the password input.
3. Effect drawing
4, Code: Details are in the comments inside the
/*author:conowen * date:2012.4.2 * * * * * * * Package com.conowen.remeberPwd;
Import android.app.Activity;
Import android.content.SharedPreferences;
Import Android.os.Bundle;
Import android.text.Editable;
Import Android.text.InputType;
Import Android.text.TextWatcher;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.ArrayAdapter;
Import Android.widget.AutoCompleteTextView;
Import Android.widget.Button;
Import Android.widget.CheckBox;
Import Android.widget.EditText;
Import Android.widget.Toast;
public class Remeberpwdactivity extends activity {Autocompletetextview Cardnumauto;
EditText Passwordet;
Button LOGBT;
CheckBox SAVEPASSWORDCB;
Sharedpreferences sp;
String Cardnumstr;
String Passwordstr; /** called the activity is a.
* * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main); Cardnumauto = (AutocomplEtetextview) Findviewbyid (R.id.cardnumauto);
Passwordet = (edittext) Findviewbyid (R.id.passwordet);
LOGBT = (Button) Findviewbyid (R.ID.LOGBT);
SP = this.getsharedpreferences ("Passwordfile", mode_private);
SAVEPASSWORDCB = (CheckBox) Findviewbyid (R.ID.SAVEPASSWORDCB); Savepasswordcb.setchecked (TRUE);//default to Remember Password Cardnumauto.setthreshold (1)//Enter 1 letters to start automatically prompt Passwordet.setinputtype ( Inputtype.type_class_text |
Inputtype.type_text_variation_password); The hidden password is Inputtype.type_text_variation_password, that is, the 0X81//display password is Inputtype.type_text_variation_visible_password, That is, 0x91 Cardnumauto.addtextchangedlistener (new Textwatcher () {@Override public void ontextchanged (charsequence s, int start, int before, int count) {//TODO auto-generated method stub string[] Allusername = new String[sp.geta
ll (). Size ()];//sp.getall (). Size () returns the number of key values to Allusername = Sp.getall (). Keyset (). ToArray (new string[0]);
Sp.getall () returns a hash map//keyset () to get a set of the keys. //hash map is composed of Key-value arrayadapter<string> adapter = new Arrayadapter<string> (remeberpwdactivity.th is, Android.
R.layout.simple_dropdown_item_1line, Allusername); Cardnumauto.setadapter (adapter)//Set data adapter} @Override public void beforetextchanged (charsequence s, int start, int count, int after) {//TODO auto-generated a stub} @Override public void aftertextchanged (EDITABL e s) {//TODO auto-generated Method Stub Passwordet.settext (sp.getstring (Cardnumauto.gettext (). toString (), ""));
/Automatic input password}}); Login Logbt.setonclicklistener (new Onclicklistener () {@Override public void OnClick (View v) {//TODO Auto-gene
Rated method Stub cardnumstr = Cardnumauto.gettext (). toString ();
Passwordstr = Passwordet.gettext (). toString (); if (!) ( (Cardnumstr.equals ("Test")) && (passwordstr. Equals ("Test"))) {Toast.maketext (remeberpwdactivity.this, "password Error, please re-enter ", Toast.length_short". Show ();
else {if (savepasswordcb.ischecked ()) {//login succeeded to save Password Sp.edit (). putstring (Cardnumstr, PASSWORDSTR). commit ();
Toast.maketext (Remeberpwdactivity.this, "login successful, getting user data ...", Toast.length_short). Show ();
Jump to another activity//Do something}}}); }
}
Layout file: Main.xml
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical " > <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_gravit Y= "Center_horizontal" android:text= "Simple login Demo" android:textsize= "25px"/> <linearlayout xmlns:android= "http
://schemas.android.com/apk/res/android "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " android:gravity= "center" android:orientation= "vertical" > <linearlayout android:layout_width= "250dip" Andr oid:layout_height= "Wrap_content" android:layout_marginleft= "10DP" android:layout_marginright= "10DP" Android:
layout_margintop= "15DP" android:orientation= "vertical" > <linearlayout android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android:orientation= "Horizontal" > <linearlayout android:layout_width= "fill_parent" android:layout_height= "80px" a ndroid:orientation= "vertical" > <linearlayout android:layout_width= "fill_parent" android:layout_height= "4 0px "android:orientation=" horizontal "> <textview android:id=" @+id/tv_account "Android:layout_width=" W Rap_content "android:layout_height=" wrap_content "android:layout_marginright=" 10DP "android:text=" Username: "Andr Oid:textsize= "15px"/> <autocompletetextview android:id= "@+id/cardnumauto" android:layout_width= "Fill_par"
Ent "android:layout_height=" 40px "> </AutoCompleteTextView> </LinearLayout> <linearlayout Android:layout_width= "Fill_parent" android:layout_height= "40px" android:orientation= "Horizontal" > <Te
Xtview android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_marginright= "10DP" android:text= "User password:"
Android:textsize= "15px"/> <edittext android:id= "@+id/passwordet" android:layout_width= "Fill_parent" android:layout_height= "40px" > </EditText> </LinearLayout> </LinearLayout> </linearla yout> <linearlayout android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:orien tation= "Horizontal" > <checkbox android:id= "@+id/savepasswordcb" android:layout_width= "Wrap_content" an
droid:layout_height= "Wrap_content" android:layout_marginleft= "20DP" android:text= "Remember Password" > </CheckBox> <button android:id= "@+id/logbt" android:layout_width= "100px" android:layout_height= "Wrap_content" Android: layout_marginleft= "40DP" android:layout_marginright= "10DP" android:text= "Login" > </Button> </linearlay out> </LinearLayout> </LinearLayout> </LinearLayout>
Sharedpreferences file, under/data/data/package name/shared_prefs folder
<?xml version= ' 1.0 ' encoding= ' utf-8 ' standalone= ' yes '?>
<map>
<string name= ' test ' >test </string>
<string name= "test2" >test</string>
<string name= "Test1" >test</ String>
This is the entire content of this article, I hope to learn more about Android software programming help.