SharedPreferences的使用

SharedPreference作用:
提供了一种轻量级的数据存取方法,主要存储数据比较少的配置信息。它以“key-value”对(类似于Map)的方式将数据保存在一个XML配置文件中。
存取数据方法:使用来自于andorid.content包接口
取数据使用:SharedPreferences接口
存数据使用:SharedPreferences.Editor接口

Context.getSharedPreferences(String name,int mode)方法得到SharedPreferences接口。
该方法的第一个参数是文件名称,第二个参数是操作模式。操作模式有三种:
MODE_PRIVATE(私有)
MODE_WORLD_READABLE(可读)
MODE_WORLD_WRITEABLE(可写)

int MODE= MODE_WORLD_READABLE+ MODE_WORLD_WRITEABLE

Editor的使用

SharedPreferences接口本身并没有提供写入数据的方法,而是通过SharedPreferences内部接口. SharedPreferences调用edit()方法获取它所对应的Editor对象,该对象中提供了保存数据的方法,
 SharedPreferences.Editor editor = getSharedPreferences(TEMP_INFO, MODE_WORLD_WRITEABLE).edit();  

SharedPreferences.Editor接口常用的方法

代码:

同个应用程序中
SharedPreferences.Editor editor;//全局变量,用来写
SharedPreferences perfer;//全局变量,用来读
int mode=MODE_WORLD_READABLE+MODE_WORLD_WRITEABLE;//这个是可以在不同应用程序之间可读和可写

perfer=getSharedPreferences("syj",mode);//第一个参数为创建一个文件名为syj,具体目录在data/data下,第二个参数是读写模式(这里是可以在不通应用程序之间可读可写)
editor=perfer.edit();//创建可编辑对象
editor.putString("name", "hello java");//写入键值对,类似MAP
editor.commit();//提交修改
     
String data = perfer.getString("name", null);//读要显示的内容
在不同的Activity下,读内容的方法
SharedPreferences sharedata = getSharedPreferences("syj", 0);
String data = sharedata.getString("name", null);

SharedPreferences的存储位置和格式

DDMS的File Explore面板展开文件浏览树
SharedPreferences数据总是保存在/data/data/<package-name>/shared_prefs目录
SharedPreferences总是已XML格式保存
文件以<map>为根元素,每个子元素代表一个key-value对

具体例子,实现一个应用程序的注册,把注册信息用SharePerference保存起来,按下按钮可以读取

首先看布局文件main.xml

View Code
 1 <?xml version="1.0" encoding="utf-8"?>
2 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:id="@+id/loginForm"
4 android:orientation="vertical"
5 android:layout_width="fill_parent"
6 android:layout_height="fill_parent"
7 >
8 <TableRow>
9 <TextView
10 android:layout_width="fill_parent"
11 android:layout_height="wrap_content"
12 android:text="用户名:"
13 android:textSize="10pt"
14 />
15 <!-- 输入用户名的文本框 -->
16 <EditText
17 android:id="@+id/edtzh"
18 android:layout_width="fill_parent"
19 android:layout_height="wrap_content"
20 android:hint="请填写登录帐号"
21 android:selectAllOnFocus="true"
22 />
23 </TableRow>
24 <TableRow>
25 <TextView
26 android:layout_width="fill_parent"
27 android:layout_height="wrap_content"
28 android:text="密码:"
29 android:textSize="10pt"
30 />
31 <!-- 输入密码的文本框 -->
32 <EditText
33 android:id="@+id/edtmm"
34 android:layout_width="fill_parent"
35 android:layout_height="wrap_content"
36 android:password="true"
37 />
38 </TableRow>
39 <TableRow>
40 <TextView
41 android:layout_width="fill_parent"
42 android:layout_height="wrap_content"
43 android:text="电话号码:"
44 android:textSize="10pt"
45 />
46 <!-- 输入电话号码的文本框 -->
47 <EditText
48 android:id="@+id/edtdh"
49 android:layout_width="fill_parent"
50 android:layout_height="wrap_content"
51 android:hint="请填写您的电话号码"
52 android:selectAllOnFocus="true"
53 android:phoneNumber="true"
54 />
55 </TableRow>
56 <Button
57 android:id="@+id/btnzc"
58 android:layout_width="wrap_content"
59 android:layout_height="wrap_content"
60 android:text="注册"
61 />
62
63 <TableRow
64 android:id="@+id/tableRow1"
65 android:layout_width="wrap_content"
66 android:layout_height="wrap_content" >
67
68
69 <Button
70 android:id="@+id/btndl"
71 android:layout_width="wrap_content"
72 android:layout_height="wrap_content"
73 android:layout_gravity="center"
74 android:text="登 录" />
75
76
77 <Button
78 android:id="@+id/btndq"
79 android:layout_width="wrap_content"
80 android:layout_height="wrap_content"
81 android:layout_gravity="right"
82 android:text="读 取" />
83
84 </TableRow>
85
86 </TableLayout>

第二步:编写具体实现功能,主要是可以读,可写

View Code
 1 package cn.edu.zwu.tel;
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.View.OnClickListener;
8 import android.widget.Button;
9 import android.widget.EditText;
10 import android.widget.Toast;
11
12 public class SharedPreTest01Activity extends Activity {
13 SharedPreferences perfer;
14 SharedPreferences.Editor editor;
15 EditText edtzh,edtmm,edtdh;
16 Button btnzc,btndl,btndq;
17 Toast toast;
18 int mode=MODE_WORLD_READABLE+MODE_WORLD_WRITEABLE;
19 @Override
20 public void onCreate(Bundle savedInstanceState) {
21 super.onCreate(savedInstanceState);
22 setContentView(R.layout.main);
23 perfer=getSharedPreferences("lgs000",mode);
24 editor=perfer.edit();
25 edtzh=(EditText)findViewById(R.id.edtzh);
26 edtmm=(EditText)findViewById(R.id.edtmm);
27 edtdh=(EditText)findViewById(R.id.edtdh);
28 btnzc=(Button)findViewById(R.id.btnzc);
29 btndl=(Button)findViewById(R.id.btndl);
30 btndq=(Button)findViewById(R.id.btndq);
31
32 btnzc.setOnClickListener(myListener);
33 btndl.setOnClickListener(myListener);
34 btndq.setOnClickListener(myListener);
35 }
36 OnClickListener myListener=new OnClickListener() {
37
38 @Override
39 public void onClick(View v) {
40 String zh=edtzh.getText().toString();
41 String mm=edtmm.getText().toString();
42 String dh=edtdh.getText().toString();
43 switch(v.getId())
44 {
45 case R.id.btnzc:
46 editor.putString("account", zh);
47 editor.putString("password", mm);
48 editor.putString("phone", dh);
49 editor.commit();
50 toast=Toast.makeText(SharedPreTest01Activity.this,
51 "你的帐号:"+zh+
52 " 密码:"+mm+
53 " 电话:"+dh, 5000);
54 toast.show();
55 edtzh.setText("");
56 edtmm.setText("");
57 edtdh.setText("");
58 return;
59 case R.id.btndl:
60 if((!zh.equals("")&&zh!=null)&&(!mm.equals("")&&zh!=null))
61 if(zh.equals(perfer.getString("account",null))&& mm.equals(perfer.getString("password",null)))
62 toast=Toast.makeText(SharedPreTest01Activity.this,
63 "登录成功!欢迎 "+zh+" 回来!!", 5000);
64 else
65 toast=Toast.makeText(SharedPreTest01Activity.this,
66 "帐号或密码错误,请重新输入!!", 5000);
67 else
68 toast=Toast.makeText(SharedPreTest01Activity.this,
69 "帐号或密码为空,请检查输入!!", 5000);
70 toast.show();
71 return;
72 case R.id.btndq:
73 if(perfer.contains("account")&&perfer.contains("password")&&perfer.contains("phone"))
74 {
75 edtzh.setText(perfer.getString("account",null));
76 edtmm.setText(perfer.getString("password",null));
77 edtdh.setText(perfer.getString("phone",null));
78 }
79 else
80 {
81 toast=Toast.makeText(SharedPreTest01Activity.this,
82 "配置信息不存在或不完整!", 5000);
83 toast.show();
84 }
85 return;
86 }
87
88 }
89 };
90 }

效果图:



posted on 2012-03-08 15:21  forrest001  阅读(1779)  评论(0编辑  收藏  举报

导航