There are many applications will have the ability to save passwords and accounts, such as QQ. Next talk about using Sharedpreferences to save passwords and accounts, perhaps some people will consider the database, but I personally think that for saving simple data, the database used is overqualified, sharedpreferences more lightweight
Write the layout first, with only two input boxes and a button.
<EditTextAndroid:id= "@+id/number"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:inputtype= "Number" /> <EditTextAndroid:id= "@+id/password"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:inputtype= "Textpassword" /> <ButtonAndroid:id= "@+id/save"Android:text= "Save"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" />
Get control
Private EditText number; Private EditText password; Private Button save; = (EditText) Findviewbyid (r.id.number); = (EditText) Findviewbyid (R.id.password); = (Button) Findviewbyid (R.id.save);
After getting the control, you also get sharedpreferences, the first parameter is the saved file name, the second is the saved model, the file is read when it exists, and if it does not exist, it is created
Private Sharedpreferences sp;
The first parameter is the saved file name, the second is the saved model, when the file exists on the read, if it does not exist create SP = getsharedpreferences ("info", mode_private);
Add button click event, click button to save account number and password
Save.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {//Get the input box account number and passwordString Numberstr =Number.gettext (). toString (). Trim (); String Passwordstr=Password.gettext (). toString (). Trim (); //determines whether the empty if(Numberstr.isempty () | |Passwordstr.isempty ()) {Toast.maketext (Getapplicationcontext (),"Account or password cannot be empty", Toast.length_short). Show (); }Else { //Get editorSharedpreferences.editor Editor =Sp.edit (); //Input ContentEditor.putstring ("number", NUMBERSTR); Editor.putstring ("Password", PASSWORDSTR); //must be submitted before it takes effect, or you can use the ApplyEditor.commit (); Toast.maketext (Getapplicationcontext (),"Saved successfully", Toast.length_short). Show (); } } });
When we save the account and password, we want to write the password and account directly when we open the app for the second time, and get the data when loading the page.
// gets the content of the info file, the first parameter is the key to save, and the second is the default value if not obtained String numberStr1 = sp.getstring ("number", ""); = sp.getstring ("Password", ""); Number.settext (NUMBERSTR1); Password.settext (PASSWORDSTR2);
This info.xml file is saved in the data/data/package name/shared_prefs/info.xml and can be seen in XML format.
Finally, I'll take the whole idea.
Save
① obtained sharedpreferences by getsharedpreferences ("filename", mode)
② get editor via Sp.edit ()
③ using editor call PUTXXX (Key,value) to save data
④ use editor to invoke apply () or commit () to take effect
Read
① obtained sharedpreferences by getsharedpreferences ("filename", mode)
② direct access to data via Sp.getxxx (Key,defvalue)
Android uses sharedpreferences to save account password