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方法來提交。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

