Android Learning Summary--sharedpreferences

Source: Internet
Author: User

Sharepreferences storage, just lightweight data storage, XML format data display way.
The simple storage steps are as follows:
One: Get Sharepreferences Object
1.SharedPreferences pref = getsharedpreferences (file name, access rights);
Or
Preferencemanager.getdefaultsharedpreferences (this);
Two: Method instances for creating objects
Pref. Editor editor = Pref.edit ();
Three: Add data to the file
Editor.putxxxx (Key,value);
The data type of XXXX generation value.
IV: Submit Editor
Editor.commit ();


// Get Sharedpreferences Object Sharedpreferences spf=getpreferences (mode_private); // Get Sharedpreferences.editor Object Sharedpreferences.editor edit=spf.edit (); // The putxxx method of the editor interface is used to save Key-value to which XXX represents different data types edit.putstring ("name", "Yang"); // save Key-value Edit.commit () by the Commit method of the editor interface;

Gets the stored value:

Spf.getstring ("name", "");

 PackageCom.example.sharedpreferencedemo1;Importandroid.content.SharedPreferences;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.CheckBox;ImportAndroid.widget.EditText;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classMainactivityextendsAppcompatactivityImplementsview.onclicklistener{//private TextView TV;    PrivateEditText user,pwd; PrivateCheckBox Saveuser; PrivateButton Login; PrivateSharedpreferences SPF; PrivateSharedpreferences.editor Editor; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); User=(EditText) Findviewbyid (R.id.et_user); PWD=(EditText) Findviewbyid (R.ID.ET_PWD); Saveuser=(CheckBox) Findviewbyid (R.id.cb_saveuser); Login=(Button) Findviewbyid (R.id.btn_login); Login.setonclicklistener ( This); SPF=getsharedpreferences ("Mypref", mode_private); Editor=Spf.edit (); if(Spf.getstring ("name", ""). Equals ("") {saveuser.setchecked (false); }Else{saveuser.setchecked (true); User.settext (Spf.getstring ("Name", "" ")); }//tv= (TextView) Findviewbyid (r.id.tv);////        //Get Sharedpreferences Object//sharedpreferences spf=getpreferences (mode_private);//        //Get Sharedpreferences.editor Object//sharedpreferences.editor Edit=spf.edit ();//        //The putxxx method of the editor interface is used to save Key-value to which XXX represents different data types//edit.putstring ("name", "Yang");//        //save Key-value through the commit method of the editor interface//edit.commit ();//Tv.settext (spf.getstring ("name", "" "));} @Override Public voidOnClick (View v) {//gets the user name and password of the interface, trim () method to remove the input spacesString name=User.gettext (). toString (). Trim (); String PWD=Pwd.gettext (). toString (). Trim (); Switch(V.getid ()) { CaseR.id.btn_login:if(Name.equals ("Yxch") &&pwd.equals ("123456"))){                    if(saveuser.ischecked ()) {editor.putstring ("Name", name);                    Editor.commit (); }                    Else{editor.remove (name);                    Editor.commit (); } toast.maketext ( This, "Login Successful! ", Toast.length_short). Show (); }Else{Toast.maketext ( This, "User name: yxch Password: 123456", Toast.length_short). Show (); }                 Break; }    }}

Android Learning Summary--sharedpreferences

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.