一、概述
對於SharedPreferences,我吧它理解為一種簡單的資料庫,它可以把資料像檔案一樣存放在系統裡面,也可以讀出來,但它存放的只是一些簡單的數值對,如key-values的形式。如果某些應用需要在退出時儲存一些簡單的資料以便再次被開啟時恢複所需的資料,那麼就可以用SharedPreferences來實現。
二、要求
編寫一個簡單的應用,要求該應用退出時能儲存某些所需要的資料,當應用再次開啟時恢複這些資料。
三、實現
建立工程MyShared,修改/res/layout/main.xml檔案,在裡面添加一個EditText,兩個Button和一個TextView,完整的main.xml檔案如下:
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 <EditText
8 android:id="@+id/edittext"
9 android:layout_width="fill_parent"
10 android:layout_height="wrap_content"
11 android:hint="請輸入內容"
12 />
13
14 <Button
15 android:id="@+id/wbutton"
16 android:layout_width="fill_parent"
17 android:layout_height="wrap_content"
18 android:text="寫入"
19 />
20
21 <Button
22 android:id="@+id/rbutton"
23 android:layout_width="fill_parent"
24 android:layout_height="wrap_content"
25 android:text="讀取"
26 />
27
28 <TextView
29 android:id="@+id/textview"
30 android:layout_width="fill_parent"
31 android:layout_height="wrap_content"
32 android:gravity="center_horizontal"
33 android:textSize="20dip"
34 />
35
36 </LinearLayout>
接著,修改MySharedActivity.java檔案,主要定義了一個SharedPreferences對象和一個SharedPreferences.Editor對象,實現兩個按鈕的監聽,完整的MySharedActivity.java檔案如下:
1 package com.nan.shared;
2
3 import android.app.Activity;
4 import android.content.SharedPreferences;
5 import android.os.Bundle;
6 import android.view.View;
7 import android.widget.Button;
8 import android.widget.EditText;
9 import android.widget.TextView;
10 import android.widget.Toast;
11
12 public class MySharedActivity extends Activity
13 {
14 private EditText mEditText = null;
15 private Button writeButton = null;
16 private Button readButton = null;
17 private TextView mTextView = null;
18
19 private SharedPreferences mSharedPreferences = null;
20 private SharedPreferences.Editor mEditor = null;
21
22 /** Called when the activity is first created. */
23 @Override
24 public void onCreate(Bundle savedInstanceState)
25 {
26 super.onCreate(savedInstanceState);
27 setContentView(R.layout.main);
28
29 mEditText = (EditText)this.findViewById(R.id.edittext);
30 writeButton = (Button)this.findViewById(R.id.wbutton);
31 readButton = (Button)this.findViewById(R.id.rbutton);
32 mTextView = (TextView)this.findViewById(R.id.textview);
33
34 mSharedPreferences = getSharedPreferences("myshare",MODE_PRIVATE);
35 mEditor = mSharedPreferences.edit();
36
37 writeButton.setOnClickListener(new View.OnClickListener()
38 {
39
40 @Override
41 public void onClick(View v)
42 {
43 // TODO Auto-generated method stub
44 //獲得EditText的內容
45 String text = mEditText.getText().toString();
46 //修改
47 mEditor.putString("hehe", text);
48 //提交修改
49 mEditor.commit();
50 displayToast("寫入成功!");
51 }
52 });
53
54 readButton.setOnClickListener(new View.OnClickListener()
55 {
56
57 @Override
58 public void onClick(View v)
59 {
60 // TODO Auto-generated method stub
61 //擷取所儲存的資料
62 String text = mSharedPreferences.getString("hehe", null);
63 //把資料顯示出來
64 mTextView.setText(text);
65 }
66 });
67
68 }
69 //顯示Toast函數
70 private void displayToast(String s)
71 {
72 Toast.makeText(MySharedActivity.this, s, Toast.LENGTH_SHORT).show();
73 }
74
75 }
好了,運行它,如下:
接著輸入一行數字,然後點擊“寫入”,如下:
退出應用,重新運行它並點擊“讀取”按鈕,效果如下:
可以看到之前儲存的資料被顯示出來了。
OK,完成。