Android采用SharedPreferences保存数据

使用SharedPreferences在程序的数据空间中生成xml文档来保存数据

基本操作:

 1 package com.hu.data;
 2 
 3 import android.app.Activity;
 4 import android.content.SharedPreferences;
 5 import android.content.SharedPreferences.Editor;
 6 import android.os.Bundle;
 7 import android.view.View;
 8 import android.view.View.OnClickListener;
 9 import android.widget.Button;
10 import android.widget.EditText;
11 
12 public class ShDemoActivity extends Activity {
13     
14     private EditText etName,etAge,etScore;
15     private Button btWrite,btRead;
16     private SharedPreferences sharedPrefrences;
17     private Editor editor;
18   
19     @Override
20     public void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.main);
23         
24         etName = (EditText) findViewById(R.id.editTextName);//得到控件
25         etAge = (EditText) findViewById(R.id.editTextAge);
26         etScore = (EditText) findViewById(R.id.editTextScore);
27         btWrite = (Button) findViewById(R.id.buttonWrite);
28         btRead = (Button) findViewById(R.id.buttonRead);
29         
30         sharedPrefrences = this.getSharedPreferences("user", MODE_WORLD_READABLE);//得到SharedPreferences,会生成user.xml
31         editor = sharedPrefrences.edit();
32         
33         btWrite.setOnClickListener(new OnClickListener() {//写入按钮事件
34             
35             public void onClick(View arg0) {
36                 String name = etName.getText().toString();
37                 int age = Integer.parseInt(etAge.getText().toString());
38                 float score = Float.parseFloat(etScore.getText().toString());//获取用户输入数据
39                 editor.putString("name", name);
40                 editor.putInt("age", age);
41                 editor.putFloat("score", score);//将数据写入xml
42                 editor.commit();//提交
43             }
44         });
45         
46         btRead.setOnClickListener(new OnClickListener() {//读出按钮事件
47             
48             public void onClick(View v) {
49                 String name = sharedPrefrences.getString("name", null);
50                 int age = sharedPrefrences.getInt("age", 0);
51                 float score = sharedPrefrences.getFloat("score", 60.0f);//将数据读出
52                 etName.setText(name);
53                 etAge.setText(Integer.toString(age));
54                 etScore.setText(Float.toString(score));//显示数据
55             }
56         });
57         
58     }
59 }


布局文件为:

 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 
 7     <LinearLayout
 8         android:layout_width="match_parent"
 9         android:layout_height="wrap_content" >
10 
11         <TextView
12             android:id="@+id/textView1"
13             android:layout_width="wrap_content"
14             android:layout_height="wrap_content"
15             android:text="姓名:"
16             android:textAppearance="?android:attr/textAppearanceLarge" />
17 
18         <EditText
19             android:id="@+id/editTextName"
20             android:layout_width="wrap_content"
21             android:layout_height="wrap_content"
22             android:layout_weight="1" >
23 
24             <requestFocus />
25         </EditText>
26     </LinearLayout>
27 
28     <LinearLayout
29         android:layout_width="match_parent"
30         android:layout_height="wrap_content" >
31 
32         <TextView
33             android:id="@+id/textView2"
34             android:layout_width="wrap_content"
35             android:layout_height="wrap_content"
36             android:text="年龄:"
37             android:textAppearance="?android:attr/textAppearanceLarge" />
38 
39         <EditText
40             android:id="@+id/editTextAge"
41             android:layout_width="wrap_content"
42             android:layout_height="wrap_content"
43             android:layout_weight="1" />
44     </LinearLayout>
45 
46     <LinearLayout
47         android:layout_width="match_parent"
48         android:layout_height="wrap_content" >
49 
50         <TextView
51             android:id="@+id/textView3"
52             android:layout_width="wrap_content"
53             android:layout_height="wrap_content"
54             android:text="分数:"
55             android:textAppearance="?android:attr/textAppearanceLarge" />
56 
57         <EditText
58             android:id="@+id/editTextScore"
59             android:layout_width="wrap_content"
60             android:layout_height="wrap_content"
61             android:layout_weight="1" />
62     </LinearLayout>
63 
64     <LinearLayout
65         android:layout_width="match_parent"
66         android:layout_height="wrap_content" >
67 
68         <Button
69             android:id="@+id/buttonWrite"
70             android:layout_width="wrap_content"
71             android:layout_height="wrap_content"
72             android:text="写入" />
73 
74         <Button
75             android:id="@+id/buttonRead"
76             android:layout_width="wrap_content"
77             android:layout_height="wrap_content"
78             android:text="读出" />
79     </LinearLayout>
80 
81 </LinearLayout>


操作界面:

保存的内容为:

1 <?xml version='1.0' encoding='utf-8' standalone='yes' ?>
2 <map>
3 <float name="score" value="89.22" />
4 <string name="name">Steve</string>
5 <int name="age" value="21" />
6 </map>

 SharePreferences存储数据是通过获取Editor编辑器对象来操作的。
插入数据:
调用Editor.putxxxx方法,两个参数分别为键和值。
获取数据:
调用Editor.getxxxx方法,两个参数分别为键和不存在指定键时的默认值。
删除数据:
调用Editor.remove方法,参数为指定的键。
清空所有数据:
调用Editor.clear方法
上述所有方法调用都要执行Editor.commit方法来提交。

posted @ 2012-06-15 20:35  丛林听雨  阅读(4816)  评论(0编辑  收藏  举报