SharedPreferences: 是个接口,用来存储KEY/VALUE数据!http://developer.android.com/reference/android/content/SharedPreferences.html

它的使用很简单分三步:

  1. 获取对象:getSharedPreferences(String filename, int mode)
    1. filename: 指定存储的文件名
    2. mode:文件的操作方式: 这些静态常量定义在Activity中!更加详细的解释,请去查询API(这是一个好习惯)。
  2. 调用edit()方法获得SharedPreferences.Editor对象,之后Invoke putXXX系列方法,进行存储。
  3. Invoke commit() 进行保存。

解惑:

  1. Q: 文件存储在什么地方? 答:adb shell 之后,进入/data/data/your_package_name/shared_prefs 这个目录里。
  2. Q: 文件名要指定存储格式么? 答:完全不需要指定,因为存储到shared_prefs目录中,文件的后缀格式为:.xml;是大家熟悉的xml文件格式。
  3. Q: 如果不小心指定的文件的存储格式:比如 demo.xml,会怎么样? 答:没有关系!在shared_prefs目录,将会看到demo.xml.xml;系统会为每个文件名自动加后缀格式。
  4. Q: 如何查看文件? 答:adb shell 之后,cd /data/data/your_package_name/shared_prefs 这个目录里; 另外可以在Eclipse中DDMS中的File explorer中查看!

代码样例:

layout file:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical"
 6     android:paddingLeft="@dimen/activity_vertical_padding" >
 7 
 8     <TextView
 9         android:layout_width="fill_parent"
10         android:layout_height="wrap_content"
11         android:text="@string/username"
12         android:textSize="@dimen/textview_20dp" />
13 
14     <EditText
15         android:id="@+id/etName"
16         android:layout_width="@dimen/edittext_width_150dp"
17         android:layout_height="wrap_content"
18         android:layout_margin="@dimen/activity_margin"
19         android:inputType="text" />
20 
21     <TextView
22         android:layout_width="fill_parent"
23         android:layout_height="wrap_content"
24         android:text="@string/habit"
25         android:textSize="@dimen/textview_20dp" />
26 
27     <EditText
28         android:id="@+id/etHabit"
29         android:layout_width="fill_parent"
30         android:layout_height="wrap_content"
31         android:layout_margin="@dimen/activity_margin"
32         android:inputType="text" />
33 
34     <CheckBox
35         android:id="@+id/cbEmployee "
36         android:layout_width="fill_parent"
37         android:layout_height="wrap_content"
38         android:layout_margin="@dimen/activity_margin"
39         android:text="@string/isEmployee" />
40 
41     <TextView
42         android:layout_width="fill_parent"
43         android:layout_height="wrap_content"
44         android:text="@string/company"
45         android:textSize="@dimen/textview_20dp" />
46 
47     <RadioGroup
48         android:id="@+id/rgCompanyType"
49         android:layout_width="fill_parent"
50         android:layout_height="wrap_content" >
51 
52         <RadioButton
53             android:id="@+id/rbCompany1"
54             android:layout_width="fill_parent"
55             android:layout_height="wrap_content"
56             android:layout_margin="@dimen/activity_margin"
57             android:text="@string/rbCompany1" />
58 
59         <RadioButton
60             android:id="@+id/rbCompany2"
61             android:layout_width="fill_parent"
62             android:layout_height="wrap_content"
63             android:layout_margin="@dimen/activity_margin"
64             android:text="@string/rbCompany2" />
65 
66         <RadioButton
67             android:id="@+id/rbCompany3"
68             android:layout_width="fill_parent"
69             android:layout_height="wrap_content"
70             android:layout_margin="@dimen/activity_margin"
71             android:text="@string/rbCompany3" />
72     </RadioGroup>
73 
74     <Button
75         android:id="@+id/shared_pre_click"
76         android:layout_width="wrap_content"
77         android:layout_height="wrap_content"
78         android:text="@string/display" />
79 
80     <TextView
81         android:id="@+id/shared_pre_text"
82         android:layout_width="wrap_content"
83         android:layout_height="wrap_content"
84         android:hint=""
85         android:textIsSelectable="false" />
86 
87 </LinearLayout>
View Code

 

Note: strings文件,dimens文件可以自定义,这里不给出!

Demo code:

  1 package com.ringcentral.demowork.storedata;
  2 
  3 import android.app.Activity;
  4 import android.content.SharedPreferences;
  5 import android.os.Bundle;
  6 import android.view.View;
  7 import android.view.Window;
  8 import android.widget.Button;
  9 import android.widget.CheckBox;
 10 import android.widget.EditText;
 11 import android.widget.RadioButton;
 12 import android.widget.RadioGroup;
 13 import android.widget.TextView;
 14 import android.widget.Toast;
 15 
 16 import com.ringcentral.demowork.R;
 17 
 18 public class UsingSharedPreferences extends Activity {
 19     private SharedPreferences spfPreferences = null;
 20     private final String fileName = "spfDemofile.xml";
 21 
 22     private EditText edName;
 23     private EditText etHabit;
 24     private CheckBox cbEmployee;
 25 
 26     private RadioGroup rGroup;
 27 //    private RadioButton one;
 28     private RadioButton two;
 29 //    private RadioButton three;
 30 
 31     private Button displayButton;
 32     private TextView displayTextView;
 33     
 34     @Override
 35     public void onCreate(Bundle savedInstanceState) {
 36         super.onCreate(savedInstanceState);
 37         this.requestWindowFeature(Window.FEATURE_NO_TITLE);
 38         setContentView(R.layout.shared_preferences);
 39         spfPreferences = getSharedPreferences(fileName, MODE_PRIVATE);
 40         init();
 41         
 42         displayButton.setOnClickListener(new View.OnClickListener() {
 43 
 44             @Override
 45             public void onClick(View v) {
 46                 // TODO Auto-generated method stub
 47                 SharedPreferences.Editor editor = spfPreferences.edit();
 48                 if (edName.getText().toString() == null
 49                         || edName.getText().toString().equalsIgnoreCase("")) {
 50                     Toast.makeText(UsingSharedPreferences.this, "请设置姓名",
 51                             Toast.LENGTH_LONG).show();
 52                     displayTextView.setText("");
 53                     return;
 54                 }
 55                 if (!cbEmployee.isChecked()) {
 56                     rGroup.clearCheck();
 57                     editor.putInt("radio", -1);
 58                 } 
 59                 editor.putInt("radio", rGroup.getCheckedRadioButtonId());
 60                 editor.putString("Name", edName.getText().toString());
 61                 editor.putString("Habit", etHabit.getText().toString());
 62                 editor.putBoolean("check", cbEmployee.isChecked());
 63 
 64                 editor.commit();
 65                 StringBuilder sb = new StringBuilder();
 66                 
 67                 sb.append(spfPreferences.getString("Name", "ADMIN"))
 68                 .append("\t")
 69                 .append(spfPreferences.getString("Habit", "无"))
 70                 .append("\t")
 71                 .append(spfPreferences.getBoolean("check", false))
 72                 .append("\t");
 73                 
 74                 int temp = spfPreferences.getInt("radio", -1);
 75                 if(temp==-1){
 76                     if(cbEmployee.isChecked()){
 77                         Toast.makeText(UsingSharedPreferences.this, "请选择单位性质",
 78                                 Toast.LENGTH_LONG).show();
 79                         displayTextView.setText("");
 80                         return;
 81                     }
 82                     sb.append("\t").append(" 您未上班哦!");
 83                     
 84                 }else{
 85                     RadioButton tempButton = (RadioButton) findViewById(spfPreferences
 86                             .getInt("radio", R.id.rbCompany2));
 87                     sb.append(tempButton.getText().toString());
 88                 }
 89                 
 90                 
 91                 displayTextView.setText(sb.toString());
 92             }
 93         });
 94 
 95     }
 96 
 97     private void init() {
 98         edName = (EditText) findViewById(R.id.etName);
 99         etHabit = (EditText) findViewById(R.id.etHabit);
100         cbEmployee = (CheckBox) findViewById(R.id.cbEmployee);
101 //        one = (RadioButton) findViewById(R.id.rbCompany1);
102         two = (RadioButton) findViewById(R.id.rbCompany2);
103 //        three = (RadioButton) findViewById(R.id.rbCompany3);
104         rGroup = (RadioGroup) findViewById(R.id.rgCompanyType);
105         displayButton = (Button) findViewById(R.id.shared_pre_click);
106         displayTextView = (TextView) findViewById(R.id.shared_pre_text);
107 
108     }
109 
110     @Override
111     public void onStop() {
112         SharedPreferences.Editor editor = spfPreferences.edit();
113         editor.putString("Name", edName.getText().toString());
114         editor.putString("Habit", etHabit.getText().toString());
115 
116         editor.putBoolean("check", cbEmployee.isChecked());
117         editor.putInt("radio", rGroup.getCheckedRadioButtonId());
118 
119         editor.commit();
120         super.onStop();
121     }
122 
123 }

 

Note: 通常把数据存储放在onStop方法中,这里给出了二个方式:一是在onStop中存储,二是为方便查看效果添加了一个Button并且对Button事件进行所应的处理,当前Button后,会再一下行显示数据。

效果如下:

下一篇讲解:File 存储!!!下周一见!!!周末愉快!!!

 

 

posted on 2013-05-31 17:30  idylan  阅读(1018)  评论(0编辑  收藏  举报