2012年7月25日 星期三

(Android Note) 設定,取得偏好設定SharedPreferences

1. 說明: 利用SharedPreferences類別, 設定和取得偏好設定內容(姓名,生日,電話)

2. SetGetSharedPreferences.java


package com.kochi.knowhow;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class SetGetSharedPreferences extends Activity{
private EditText name;
private EditText birthday;
private EditText tel;
private Button set;
private Button get;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sharedpreferences);
        name = (EditText) findViewById(R.id.spName);
        birthday = (EditText) findViewById(R.id.spBirthday);
        tel = (EditText) findViewById(R.id.spTel);
        set = (Button) findViewById(R.id.spSet);
        get = (Button) findViewById(R.id.spGet);
       
        set.setOnClickListener(setListener); //設定監聽模式
        get.setOnClickListener(getListener); //取得監聽模式
}
//按下設定按鈕
private OnClickListener setListener = new OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

//getSharedPreferences(String name,int mode) , name為設定檔名稱
//MODE_PRIVATE : 只有此程式可以存取
//MODE_WORLD_READABLE : 其它程式也可以讀取
        //MODE_WORLD_WRITEABLE : 其它程式可以寫
//MODE_WORLD_READABLE + MODE_WORLD_WRITEABLE : 其它程式可讀取寫入
SharedPreferences setting = getSharedPreferences("SP", MODE_WORLD_READABLE + MODE_WORLD_WRITEABLE);
//把name,birthday,tel變數儲存Name,Birthday,Tel屬性, 必須要加入.commit()才會建立此檔案
setting.edit().putString("Name",name.getText().toString()).commit();
setting.edit().putString("Birthday",birthday.getText().toString()).commit();
setting.edit().putString("Tel",tel.getText().toString()).commit();

//清空EditText內容
name.setText("");
birthday.setText("");
tel.setText("");
}

};
//按下取得按鈕
private OnClickListener getListener = new OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

//getSharedPreferences(String name,int mode) , name為設定檔名稱
SharedPreferences getting = getSharedPreferences("SP", 0);
//讀取Name屬性值,再顯示EditText框架上
name.setText(getting.getString("Name", ""));
//讀取Birthday屬性值,再顯示EditText框架上
birthday.setText(getting.getString("Birthday", ""));
//讀取Tel屬性值,再顯示EditText框架上
tel.setText(getting.getString("Tel", ""));
}

};
}

3. sharedpreferences.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView android:text="姓名" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <EditText android:layout_height="wrap_content" android:text="" 
    android:id="@+id/spName" android:layout_width="match_parent" android:inputType="text"></EditText>
    <TextView android:text="生日" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <EditText android:layout_height="wrap_content" android:text="" 
    android:id="@+id/spBirthday" android:layout_width="match_parent" android:inputType="date"></EditText>
    <TextView android:text="電話" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <EditText android:layout_height="wrap_content" android:text="" 
    android:id="@+id/spTel" android:layout_width="match_parent" android:inputType="number"></EditText>
   
    <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button android:text="設定" android:id="@+id/spSet" 
      android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    <Button android:text="取得" android:id="@+id/spGet" 
      android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    </LinearLayout>
   
</LinearLayout>

4. 執行步驟
a. 輸入內容,按下設定按鈕

b. 按下設定按鈕後,清空EditText 

c. 按下取得按鈕, 會讀取在偏好設定的值

5. 偏好設定會存在
(/data/data/com.kochi.knowhow/shared-prefs/SP.xml)
以下權限為 MODE_WORLD_READABLE + MODE_WORLD_WRITEABLE : 其它程式可讀取







沒有留言:

張貼留言