The contents of this article over SP shared data storage Implementation remember the account password function
*************************************************************************************************************** ************
public class Mainactivity extends Activity {
Private Autocompletetextview Completetextview;
Private EditText EditText;
Private Sharedpreferences sp;
Private CheckBox Issavebox;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
//This method has a specific implementation below
Findmyview ();
}
//method to get data collection
Private List<string> Initnamedata () {
list<string> list = new arraylist<string> ();
map<string,?> map = Sp.getall ();
set<string> set = Map.keyset ();
//Traverse through Foreath
for (String Name:set) {
if (!name.equals ("name") &&!name.equals ("pwd") &&!name.equals ("Issave")) {
List.add (name);
}
}
return list;
}
private void Findmyview () {
Completetextview = (Autocompletetextview) Findviewbyid (r.id.text_autocomplete);
EditText = (editText) Findviewbyid (R.id.text_edit);
Issavebox = (CheckBox) Findviewbyid (R.id.checkbox_issave);
///Get Shared data object, shared data object is only responsible for reading data, writing data is completed by editor object
Parameter one: Save the file name of data, do not add suffix, automatically generate Usrmsg.xml file, exist/data/data/package name/shared_prefs Directory
/ /Parameter two: private mode
SP = getsharedpreferences ("UserInfo", mode_private);
/* This XML file is the XML file that is stored in the phone's memory via the SP, with the following data (two accounts in total, one remembering the password zhangsan,123 The other is the lisi,4567 that does not remember the password)
* <?xml version= ' 1.0 ' encoding= ' utf-8 ' standalone= ' yes '?>
<map>
<boolean name= "Issave" value= "false"/>
<string name= "Zhangsan" >123</string>
<string name= "pwd" >123</string>
<string name= "Lisi" >4567</string>
<string name= "Name" >zhangsan</string>
</map>*/
Read data from SP, set to control
If the user has chosen to remember, load the user name password to the control
Boolean issave = Sp.getboolean ("Issave", false);
Issavebox.setchecked (Issave);
if (Issave) {
String uname = sp.getstring ("name", "" ");
Completetextview.settext (uname);
String upwd = sp.getstring ("pwd", "" ");
Edittext.settext (UPWD);
}
list<string> usernamelist = Initnamedata ();//This method is to get the data set, encapsulated in the above
arrayadapter<string> adapter = new Arrayadapter<string> (This,
Android. R.layout.simple_list_item_1,
Usernamelist);
Completetextview.setadapter (adapter);
Completetextview.setonitemclicklistener (New Onitemclicklistener () {
@Override
public void Onitemclick (adapterview<?> parent, view view,
int position, long ID) {
//You can't use position to get the data, because position position to filter the corresponding data when the user enters different data is not the same.
The view of parameter two is the object that is currently clicked, using this object to get the data
/ /Because the AutoComplete control was previously set to layout with the Android system's own layout file containing only one textview, so:
TextView TextView = (TextView) view;
String name = Textview.gettext (). toString ();
After obtaining the user's chosen username, the user name gets the corresponding password and sets the value for its control, which realizes remembering the account password function.
//getstring method, parameter one is the keyword, parameter two is when the search is not the default value to fill in
String pwd = sp.getstring (name, "");
Edittext.settext (PWD);
}
});
}
public void Login (view view) {
String name = Completetextview.gettext (). toString ();
String pwd = Edittext.gettext (). toString ();
//When the OK button is clicked, the data needs to be written, which is the writing data is done by the editor object,
if (textutils.isempty (name) | | Textutils.isempty (pwd)) {
//To determine if the input data is empty or empty, then make a hint
Toast.maketext (This, "account name or password cannot be empty", Toast.length_short). Show ();
}else {
//When you click the OK button, you need to write the data, which is the write data is done by the editor object
Editor editor = Sp.edit ();
if (issavebox.ischecked ()) {
//When the user chooses to remember the account password, the option (remember password) and account password are stored separately in SP file for easy access
Editor.putboolean ("Issave", true);
Editor.putstring ("name", name);
Editor.putstring ("pwd", PWD);
}else {
Editor.putboolean ("Issave", false);
}
//When the user does not choose to remember the account password, the user entered the user name as the key, the password as a value in the SP file, easy to complete the automatic text box
Editor.putstring (name, PWD);
//When the data is stored, it must be submitted, only the data can be submitted to the SP file to be truly deposited
Editor.commit ();
}
}
}
Android Development about SP shared data storage (remember account password stored data example)