Android:sharedpreferences Remember user name and password

Source: Internet
Author: User

Reference: http://blog.csdn.net/liuyiming_/article/details/7704923

Sharedpreferences Introduction:

Sharedpreferences is a lightweight storage class on the Android platform, mainly to save some common configuration parameters, it is the use of XML files to store data, files stored in the "/data/data<package name>/shared _prefs "directory.

Usage of sharedpreferences:

Since Sharedpreferences is an interface, it does not provide the ability to write data and read data in this interface. But it is through some of the methods in its editor interface to operate the Sharedpreference, the usage is shown in the following code:

Context.getsharedpreferences (String name,int mode) to get a sharedpreferences instance

Name: Refers to the file name, does not need to add the suffix. XML, the system will be added to us automatically.

Mode: is the specified read and write mode, the value of three kinds, respectively:

Context.mode_private: Specifies that the sharedpreferences data can only be read and written by this application

Context.mode_world_readable: Specifies that the sharedpreferences data can be read by other applications but cannot be written

Context.mode_world_writeable: Specifies that the sharedpreferences data can be read and written by other applications.

The code is as follows:

Importandroid.app.Activity;ImportAndroid.content.Context;Importandroid.content.SharedPreferences;ImportAndroid.content.SharedPreferences.Editor;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.CheckBox;ImportAndroid.widget.EditText; Public classRegisteractivityextendsActivity {PrivateEditText Mname,mpassword,mphone; PrivateSharedpreferences sp; PrivateString Name,password,phone; PrivateCheckBox Auto_login,rb_password; PrivateButton sure; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);         Setcontentview (R.layout.activity_register); //Get Instance ObjectSP = This. Getsharedpreferences ("Login_info", context.mode_private); Rb_password.setchecked (true);//set default is record password statusAuto_login.setchecked (true);//Set DEFAULT is auto sign-in statusMname=(EditText) Findviewbyid (r.id.name); Mpassword=(EditText) Findviewbyid (R.id.password); Mphone=(EditText) Findviewbyid (R.id.phonenumber); Sure=(Button) Findviewbyid (r.id.sure);
Auto_login = (CheckBox) Findviewbyid (R.id.auto_log);
Rb_password = (CheckBox) Findviewbyid (R.ID.RB_PW);
Sure.setonclicklistener (NewmyButtonListener1 ()); }        classMyButtonListener1Implementsonclicklistener{@Override Public voidOnClick (View v) {//TODO auto-generated Method StubRemember (); }                }     Public voidcheck_login_state () {//to remember the state of a password multi-box          if(Sp.getboolean ("Ischeck",true)) {//get user information from the SPMname.settext (sp.getstring ("Login_name", "" ")); Mpassword.settext (Sp.getstring ("Login_password", "" ")); //determine the status of auto-login multi-marquee                 if(Sp.getboolean ("Auto_ischeck",true))                     {                     //Skip Login Interface                     }            }    }         Public voidRemember () {name=Mname.gettext (). toString (); Password=Mpassword.gettext (). toString (); Phone=Mphone.gettext (). toString (); //Remember the user name, password,Editor Editor =Sp.edit (); Editor.putstring ("Login_name", name); Editor.putstring ("Login_password", password); Editor.putstring ("Login_phone", phone);    Editor.commit (); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.register, menu); return true; }}

Android:sharedpreferences Remember user name and password

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.