存取偏好設定檔案的說明如下:
1.呼叫Context的getSharedPreferences()取得SharedPreferences
getSharedPreferences settings=getSharedPreferences(String?name, int mode)
2.呼叫SharedPreferences的edit()以取得SharedPreferences.E
ditor物件,方可編輯內容 settings.edit() .putString(“key1", stringValue) .putBoolean(“key2", booleanValue) .putInt(“key3", intValue) .commit(); 3.呼叫SharedPreferences對應的getter方法取得對應資料 String fileName = settings.getString("key1", default_fileName); 4.呼叫SharedPreferences.Editor的remove()將指定資料移除 settings.edit() .remove(key1) .commit(); @@檔案儲存於 data/data/<專案套件名稱>/shared_prefs/prefSet.xml@@
設計ch7_3_sharedpref.java
- package com.example.ch7_3_sharedpref;
- import android.os.Bundle;
- import android.app.Activity;
- import android.content.SharedPreferences;
- import android.util.Log;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.RadioButton;
- import android.widget.Toast;
- public class Ch7_3_SharedPref extends Activity {
- EditText etFile,etSec;
- RadioButton rbYes,rbNo;
- Button btn1,btn2,btn3;
- String prefName="prefSet";
- String defaultName="Image";
- boolean defaultIsAutoFocus=true;
- int defaultSec=10;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- etFile=(EditText)findViewById(R.id.editText1);
- etSec=(EditText)findViewById(R.id.editText2);
- rbYes=(RadioButton)findViewById(R.id.radioButton1);
- rbNo=(RadioButton)findViewById(R.id.radioButton2);
- btn1=(Button)findViewById(R.id.button1);
- btn2=(Button)findViewById(R.id.button2);
- btn3=(Button)findViewById(R.id.button3);
- //1.儲存設定
- btn1.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v) {
- SharedPreferences sp=getSharedPreferences(prefName,MODE_PRIVATE);
- String fileName= etFile.getText().toString();
- boolean isAutoFocus=true;
- if(rbYes.isChecked())
- isAutoFocus=true;
- else
- isAutoFocus=false;
- int sec=0;
- try{
- sec=Integer.parseInt(etSec.getText().toString());
- }catch(Exception e){
- Log.d("Ch7_3_SharedPref", "例外發生:"+e.toString());
- Toast.makeText(Ch7_3_SharedPref.this, "請輸入數字", Toast.LENGTH_LONG).show();
- }
- sp.edit()
- .putString("fileName",fileName )
- .putBoolean("isAutoFocus", isAutoFocus)
- .putInt("sec", sec)
- .commit();
- Toast.makeText(Ch7_3_SharedPref.this, "1.儲存設定", Toast.LENGTH_LONG).show();
- }});
- //2.載入設定
- btn2.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v) {
- SharedPreferences sp=getSharedPreferences(prefName,MODE_PRIVATE);
- String fileName=sp.getString("fileName", defaultName);
- etFile.setText(fileName);
- boolean isAutoFocus=sp.getBoolean("isAutoFocus", defaultIsAutoFocus);
- if(isAutoFocus)
- rbYes.setChecked(true);
- else
- rbNo.setChecked(true);
- int sec=sp.getInt("sec", defaultSec);
- etSec.setText(Integer.toString(sec));
- Toast.makeText(Ch7_3_SharedPref.this, "2.載入設定", Toast.LENGTH_LONG).show();
- }});
- //3.回復預設
- btn3.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v) {
- etFile.setText(defaultName);
- if(defaultIsAutoFocus)
- rbYes.setChecked(true);
- else
- rbNo.setChecked(true);
- etSec.setText(Integer.toString(defaultSec));
- Toast.makeText(Ch7_3_SharedPref.this, "3.回復預設", Toast.LENGTH_LONG).show();
- }});
- }
- }
復制代碼
|