Since it is the ability to remember the password, then we do not have to write from scratch, because the best practice section in the previous chapter has written a login interface, there can be reused code why not? Then first open the Broadcastbestpractice project to edit the layout of the login interface. Modify the code in the Login.xml as follows:
<tablelayout xmlns:android= "Http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "Match_parent "
android:stretchcolumns= "1" >
......
<TableRow>
<checkbox android:id= "@+id/remember_pass" android:layout_height= "Wrap_content"/>
<textview android:layout_height= "wrap_content" android:text= "Remember password"/>
</TableRow>
<TableRow>
<button android:id= "@+id/login" android:layout_height= "Wrap_content" android:layout_span= "2" android:text= " Login "/>
</TableRow>
</TableLayout>
A new control, CheckBox, is used here. This is a check box control that users can click to select and Cancel, and we use this control to indicate whether the user needs to remember the password.
Then modify the code in the loginactivity as follows:
Public class Loginactivityextends baseactivity {
Private Sharedpreferences pref;
Private Sharedpreferences.editor Editor;
Private EditText Accountedit; Privateedittext Passwordedit; Private Button login;
Private CheckBox Rememberpass;
@Override
Protected voidoncreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.login);
Pref =preferencemanager.getdefaultsharedpreferences (this); Accountedit = (EditText) Findviewbyid (R.id.account); Passwordedit = (EditText) Findviewbyid (R.id.password); Rememberpass = (CheckBox) Findviewbyid (R.id.remember_pass); Login = (Button) Findviewbyid (R.id.login);
Boolean isremember = Pref.getboolean ("Remember_password", false);
if (Isremember) {
// Set the account and password to the text box
String account = pref.getstring ("Account", ""); String Password = pref.getstring ("Password", "" "); Accountedit.settext (account); Passwordedit.settext (password);
Rememberpass.setchecked (TRUE);
}
Login.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
String account =accountedit.gettext (). toString (); String password =passwordedit.gettext (). toString ();
if (account.equals ("admin") && password.equals ("123456")) {
Editor = Pref.edit ();
If (rememberpass.ischecked ()) {// check check box is selected
Editor.putboolean ("Remember_password", true); Editor.putstring ("account", account), editor.putstring ("password", password);
} else {
Editor.clear ();
}
Editor.commit ();
Intent Intent = newintent (Loginactivity.this, Mainactivity.class);
StartActivity (Intent);
Finish ();
} else {
Toast.maketext (Loginactivity.this, "Account or Password Isinvalid", Toast.length_short). Show ();
}
}
});
}
}
As you can see, this first gets the Sharedpreferences object in the OnCreate () method, and then calls its Getboolean () method to get the value of the Remember_password key, which, of course, does not have a corresponding value at first. So the default value of false is used so that nothing happens. After the login is successful, the checkbox's IsChecked () method is called to check if the check box is selected, if the user wants to remember the password, the Remember_password is set to True, then the account and The corresponding values of the password are stored in the Sharedpreferences file and submitted. If it is not selected, simply call the clear () method to erase all the data in the Sharedpreferences file.
When the user selects the Remember password check box, and after successful login, the value of the Remember_password key is true, and if you restart the login interface again, the saved account and password will be read from the Sharedpreferences file. and fill in the text input box, then the Remember password check box is selected, so that the ability to remember the password.
Now re-run the program, you can see the interface has a memory password check box, 6.9 shows.
Figure 6.9
Then enter admin, password enter 123456, and select the Remember Password check box, click Login, will jump to mainactivity. Then send a mandatory offline broadcast in the mainactivity will let the program back to the login screen, you will find that the account password has been automatically populated on the interface, 6.10 is shown.
Figure 6.10
So we use sharedpreferences technology will remember the password function successfully implemented, you do not understand sharedpreferences more profound? However, it is important to note that the password-remembering feature implemented here is still a simple example and cannot be
Used directly in the project. Because it is very insecure to store passwords in clear text in the Sharedpreferences file, it is very easy to be stolen by others, so in a formal project you also need to combine certain cryptographic algorithms to protect passwords.
Android:sharedpreferences Implement remember Password function