Public class Mainactivity extends Appcompatactivity {
EditText Uedittext, Pedittext;
checkbox checkbox;
button button;
/**
* Attention:this is auto-generated to implement the APP indexing API.
* See Https://g.co/AppIndexing/AndroidStudio for more information.
*/
private googleapiclient client;
@Override
protected void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
Uedittext = (EditText) Findviewbyid (r.id.ed_user);
Pedittext = (EditText) Findviewbyid (R.id.ed_password);
checkbox= (CheckBox) Findviewbyid (r.id.cb_remember);
button= (Button) Findviewbyid (R.id.button);
//Use sharepreferences to remove saved data and display the data on the phone screen
//Initialize data
sharedpreferences sharedpreferences=getsharedpreferences ("config", 0);
///Remove the data, if the data is taken out of space-time, just put GetString ("", "") the second parameter is set to an empty string on the line, not in the judgment
String name=sharedpreferences.getstring ("name", "");
String password=sharedpreferences.getstring ("Password", "");
//Get tick status
boolean Checkbox=sharedpreferences.getboolean ("checkbox", false);
Uedittext.settext (name);
pedittext.settext (password);
checkbox.setchecked (checkBox);
}
//Use Sharepreferences to save data
Public void Login (view view) {
//Get password and user name
String Username=uedittext.gettext (). toString ();
String Passwowrd=pedittext.gettext (). toString ();
//Text judgment is empty, new API:TextUtils.isEmty ()
if (textutils.isempty (username) &&textutils.isempty (PASSWOWRD)) {
Toast.maketext (mainactivity.this, "User name and password cannot be empty", Toast.length_long). Show ();
}else{
System.out.println ("Fill in later");
if (checkbox.ischecked ()) {
//Save the password and user name.
//getsharedpreferences (Name,model); name generates an XML file, model: schema, readable writable, etc.
sharedpreferences sp=getsharedpreferences ("config", 0);
sharedpreferences.editor Editor=sp.edit ();
//Save the data
editor.putstring ("name", username);
editor.putstring ("password", PASSWOWRD);
//Remember the tick status
Editor.putboolean ("checkbox", checkbox.ischecked ());
//Submit data
editor.commit ();
}else{
Toast.maketext (mainactivity.this, "unchecked", Toast.length_long). Show ();
}
}
}
/**
* Steps to use for Sharepreference
* 1. Get an instance of SP
* sharepreference sp=getsharepreference (name,model);
* 2. Get editor
* Editor editor=sp.edit ();
* 3. Save Data
* editor.putstring (name, value)
* 4. Submit
* Editor.commit ();
*/
}
XML file
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical"
tools:context= "Com.hx.myapplication.MainActivity" >
<edittext
Android:id= "@+id/ed_user"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:hint= "@string/husername"/>
<edittext
Android:id= "@+id/ed_password"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:password= "true"
android:hint= "@string/hpsword"/>
<relativelayout
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content" >
<checkbox
android:layout_margintop= "20DP"
Android:id= "@+id/cb_remember"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/remember"/>
<button
Android:id= "@+id/button"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:layout_below= "@+id/cb_remember"
android:text= "@string/login"
android:onclick= "Login"/>
</RelativeLayout>
</LinearLayout>
Android remembers login status using Sharepreference interface