Reprint please mark: reproduced in http://www.cnblogs.com/Liuyt-61/p/6637515.html
---------------------------------------------------------------
Four ways to store Android data:
1, Sharedpreferences
2, SQLite
3. Content Provider
4. File
----------------------Split Line----------------------------------
First, Sharedpreferences:
1. is a lightweight data storage method.
2. Essence is based on XML file storage Key-value key value pair data
3. Usually used to store some simple configuration information, such as user name, password (appended with the instance code)
The 1>sharedpreferences object itself can only fetch data and does not support storage and modification
Editor for storage and modification
2> steps to implement storage:
① uses the activity class's getsharedpreferences to get its object, where the name of the file that stores the Key-value is specified by the first parameter of the Getsharedpreferences method.
② uses the Sharedpreferences interface's edit to obtain the Sharedpreferences.editor object.
③ the Putxxx method of the Sharedpreferences.editor interface to save the Key-value
④ is committed by saving the Key-value on the commit () method of the Sharedpreferences.editor interface.
Directly on the instance code (login interface to save the user name)
Main.xml
<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"Android:layout_width="match_parent"Android:layout_height="match_parent"android:orientation="Vertical"> <LinearLayout android:layout_width="match_parent"Android:layout_height="wrap_content"> <TextView Android:id="@+id/tv_username"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="User name"/> <EditText Android:id="@+id/et_username"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:layout_weight="1"Android:ems="Ten"Android:hint="input Username"Android:inputtype="Textpersonname"> </EditText> </LinearLayout> <LinearLayout android:layout_width="match_parent"Android:layout_height="wrap_content"> <TextView Android:id="@+id/tv_password"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="Password"/> <EditText Android:id="@+id/et_password"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:layout_weight="1"Android:ems="Ten"Android:hint="Input Password"Android:inputtype="Textpassword"/> </LinearLayout> <LinearLayout android:layout_width="match_parent"Android:layout_height="wrap_content"> <CheckBox Android:id="@+id/cb_remember"android:checked="false"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="Remember user name"/> </LinearLayout> <LinearLayout android:layout_width="match_parent"Android:layout_height="wrap_content"android:orientation="Horizontal"> <Button Android:id="@+id/btn_login"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:onclick="DoClick"Android:text="Login"/> <Button Android:id="@+id/btn_canel"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:onclick="DoClick"Android:text="Cancel"/> </LinearLayout></LinearLayout>
Mainactivity.java
Package com. Liuyt.s03_e28_sharedpreferences;import Android.app.activity;import Android.content.sharedpreferences;import Android.content.sharedpreferences.editor;import Android.os.bundle;import Android.view.view;import Android.widget.button;import android.widget.checkbox;import android.widget.edittext;import android.widget.Toast; Public classMainactivity extends Activity {PrivateButton Btn_login,btn_canel; PrivateEditText Et_username,et_password; PrivateCheckBox Cb_remenber; PrivateEditor Editor; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.main);//sharedpreferences pref = getsharedpreferences ("Mypref", mode_private);//Editor editor = Pref.edit ();//editor.putstring ("name", "Liuyt");//editor.putint ("Age",);//editor.commit ();//System.out.println (pref.getstring ("name", "" "));//System.out.println (Pref.getint ("age", 0));Btn_canel =(Button) Findviewbyid (R.id.btn_canel); Btn_login=(Button) Findviewbyid (R.id.btn_login); Et_username=(EditText) Findviewbyid (r.id.et_username); Et_password=(EditText) Findviewbyid (R.id.et_password); Cb_remenber=(CheckBox) Findviewbyid (R.id.cb_remember); Sharedpreferences pref= Getsharedpreferences ("MYPREF1", mode_private); Editor=Pref.edit (); String name= Pref.getstring ("username",""); if(Name = =NULL) {cb_remenber.setchecked (false); }Else{cb_remenber.setchecked (true); Et_username.settext (name); } } Public voidDoClick (view view) {Switch(View.getid ()) { Caser.id.btn_login:string name= Et_username.gettext (). toString (). Trim ();//Remove the leading and trailing spacesString Password =Et_password.gettext (). toString (). Trim (); if("Admin". Equals (name) &&"123456". Equals (password)) { if(cb_remenber.ischecked ()) {editor.putstring ("username", name); Editor.commit (); }Else{editor.remove ("username"); Editor.commit (); } toast.maketext (mainactivity. This,"Login Successful", Toast.length_short). Show (); }Else{toast.maketext (mainactivity). This,"Deny Login", Toast.length_short). Show (); } Break; default: Break; } } }
[Android] Data article---sharedpreferences