Typical applications:
After entering an interface, the default value is displayed (this can also be specified directly in the layout file)
Basic points
1) The files generated by Sharepreferences are ultimately present in the form of an. xml file in the/data/data/application package name/share_prefs/xxx.xml
2) sharepreferences suitable for storing data of key-value type
Basic use:
Save:
Editor editor = Sp.edit ();//Get editor editor.putstring ("name", name),///store data (not yet entered into the file) editor.putstring ("Phone", phone); Editor.putstring ("email", email); Editor.commit ();//Commit changes (similar to transactions)
Take:
SP = getsharedpreferences ("Data", mode_private);//Gets the object, by default, to the current app. The file name is Data.xml, the mode is private// SP = getpreferences (mode_ PRIVATE);//This time the generated file name is MainActivity.xmlet_name.setText (sp.getstring ("name", "")), Et_phone.settext (Sp.getstring (" Phone "," ")); Et_email.settext (sp.getstring (" email "," "));
Parsing: Why do we use Getpreferences (mode_private) to generate a file named Mainactivity.xml?? As shown in the following:
In fact, in the previous blog I mentioned that this very similar function, at the bottom of the implementation of the time, it is likely that you tune me, I tune your ... truth speaks louder than words, and there is no secret in front of source . Now we go to see the Android source code in these two functions will know ...
SOURCE Analysis:
First Look at Getsharedpreferences (String name, int mode):
@Override public sharedpreferences getsharedpreferences (String name, int mode) { return Mbase.getsharedpreferences (name, mode); }
Next, take a look at getpreferences (int mode):
/** * Retrieve a {@link sharedpreferences} object for accessing preferences * that is private to this activity.
this simply calls the underlying * {@link #getSharedPreferences (String, int)} method by passing in this activity ' s< C5/>* class name as the preferences name. * * @param mode Operating mode. Use {@link #MODE_PRIVATE} for the default * operation, {@link #MODE_WORLD_READABLE} and * {@link # Mode_world_writeable} to control permissions. * * @return Returns The Sharedpreferences instance that can be used * to retrieve and modify the pre Ference values. */Public sharedpreferences getpreferences (int mode) { return getsharedpreferences (Getlocalclassname (), mode); }
getpreferences (int mode) method used in the Getlocalclassname () This function, in fact, this function does not need to see its source code, just look at its name to know what he does: get the current activity of the name .... But now that we have seen it, let's go and see how it's written in the source code.
/** * Returns class name for this activity with the package prefix removed. * The default name used to read and write settings. * * @return The local class name. * /public string getlocalclassname () { final String pkg = Getpackagename (); Final String cls = Mcomponent.getclassname ();//Get Long class Name: Package name + class name int packagelen = Pkg.length (); if (!cls.startswith (pkg) | | cls.length () <= Packagelen | | cls.charat (packagelen)! = '. ') { return cls; } Return cls.substring (packagelen+1);//intercept the section after the package name. That is, the class name }
Example:
1, Mainactivity
Package Com.example.sharepreferencetest;import Android.os.bundle;import Android.app.activity;import Android.content.context;import Android.content.sharedpreferences;import Android.content.SharedPreferences.Editor ; Import Android.view.menu;import Android.view.view;import android.widget.edittext;public class MainActivity extends Activity {private EditText et_name;private EditText et_phone;private EditText et_email;private sharedpreferences sp;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.main); Et_name = (EditText) Findviewbyid (r.id.nameet); et_phone = (EditText) Findviewbyid (r.id.phoneet); et_email = (EditText) Findviewbyid (r.id.emailet); sp = getsharedpreferences ("Data", mode_private);//Gets the object, by default, to the current app. File name is Data.xml, mode is private//SP = Getpreferences (mode_private);//This time the generated file name is MainActivity.xmlet_name.setText (sp.getstring ("name", "")); et_ Phone.settext (sp.getstring ("Phone", "")), Et_email.settext ("E-mail", ""));public void OnClick (view view) {String name = Et_name.gettext (). toString (); String phone = Et_phone.gettext (). toString (); String email = et_email.gettext (). toString (); Editor editor = Sp.edit ();//Get editor editor.putstring ("name", name),///store data (not yet entered into the file) editor.putstring ("Phone", phone); Editor.putstring ("email", email), editor.commit ();//commit modify (similar to Transaction)} @Overridepublic Boolean Oncreateoptionsmenu (menu menu) {//Inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu ); return true;}}
2, Main.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical "&G T <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "surname Name "/> <edittext android:id=" @+id/nameet "android:layout_width=" Match_parent "Android:layout_ height= "Wrap_content" android:inputtype= "Textpersonname" > <requestfocus/> </EditText> <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= " Phone "/> <edittext android:id=" @+id/phoneet "android:layout_width=" Match_parent "android:layou t_height= "Wrap_content" android:inputtype= "Phone"/> <textview android:layout_width= "Wrap_content" android:layout_height="Wrap_content" android:text= "Mailbox"/> <edittext android:id= "@+id/emailet" android:layout_width = "Match_parent" android:layout_height= "wrap_content" android:inputtype= "textemailaddress"/> <Butto n android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:onclick= "OnClick" android:text= "Save as Default"/></linearlayout>
:
The contents of the generated Data.xml file are as follows:
<?xml version= ' 1.0 ' encoding= ' utf-8 ' standalone= ' yes '? ><map><string name= "Email" >[email protected ]</string><string name= "Phone" >13675173829</string><string name= "name" >hjd</string ></map>
SOURCE Download: http://download.csdn.net/detail/caihongshijie6/7615023