Android remembers login status using Sharepreference interface

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.