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

它的使用很简单分三步:

  1. 获取对象:getSharedPreferences(String filename, int mode)
    1. filename: 指定存储的文件名
    2. mode:文件的操作方式:Android开发之SharedPreferences 这些静态常量定义在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

相关文章: