TShopping

 找回密碼
 註冊
搜索
TShopping 精選文章 Android 手機開發 查看內容

[Android智慧型手機設計_shared preferences:存取偏好檔案的內容]

2013-7-10 19:53| 發佈者: woff| 查看: 716| 評論: 0|原作者: woff

摘要: 存取偏好設定檔案的說明如下: 1.呼叫Context的getSharedPreferences()取得SharedPreferences getSharedPreferences settings=getSharedPreferences(String?name, int mode) 2.呼叫SharedPreferences的edit()以取得S ...
 

1.jpg



存取偏好設定檔案的說明如下:


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

  1. package com.example.ch7_3_sharedpref;

  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.content.SharedPreferences;
  5. import android.util.Log;
  6. import android.view.Menu;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.RadioButton;
  12. import android.widget.Toast;

  13. public class Ch7_3_SharedPref extends Activity {
  14. EditText etFile,etSec;
  15. RadioButton rbYes,rbNo;
  16. Button btn1,btn2,btn3;
  17. String prefName="prefSet";
  18. String defaultName="Image";
  19. boolean defaultIsAutoFocus=true;
  20. int defaultSec=10;
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.main);

  25. etFile=(EditText)findViewById(R.id.editText1);
  26. etSec=(EditText)findViewById(R.id.editText2);
  27. rbYes=(RadioButton)findViewById(R.id.radioButton1);
  28. rbNo=(RadioButton)findViewById(R.id.radioButton2);
  29. btn1=(Button)findViewById(R.id.button1);
  30. btn2=(Button)findViewById(R.id.button2);
  31. btn3=(Button)findViewById(R.id.button3);
  32. //1.儲存設定
  33. btn1.setOnClickListener(new OnClickListener(){
  34. @Override
  35. public void onClick(View v) {
  36. SharedPreferences sp=getSharedPreferences(prefName,MODE_PRIVATE);
  37. String fileName= etFile.getText().toString();
  38. boolean isAutoFocus=true;
  39. if(rbYes.isChecked())
  40. isAutoFocus=true;
  41. else
  42. isAutoFocus=false;
  43. int sec=0;
  44. try{
  45. sec=Integer.parseInt(etSec.getText().toString());
  46. }catch(Exception e){
  47. Log.d("Ch7_3_SharedPref", "例外發生:"+e.toString());
  48. Toast.makeText(Ch7_3_SharedPref.this, "請輸入數字", Toast.LENGTH_LONG).show();
  49. }
  50. sp.edit()
  51. .putString("fileName",fileName )
  52. .putBoolean("isAutoFocus", isAutoFocus)
  53. .putInt("sec", sec)
  54. .commit();
  55. Toast.makeText(Ch7_3_SharedPref.this, "1.儲存設定", Toast.LENGTH_LONG).show();
  56. }});
  57. //2.載入設定
  58. btn2.setOnClickListener(new OnClickListener(){

  59. @Override
  60. public void onClick(View v) {
  61. SharedPreferences sp=getSharedPreferences(prefName,MODE_PRIVATE);
  62. String fileName=sp.getString("fileName", defaultName);
  63. etFile.setText(fileName);
  64. boolean isAutoFocus=sp.getBoolean("isAutoFocus", defaultIsAutoFocus);
  65. if(isAutoFocus)
  66. rbYes.setChecked(true);
  67. else
  68. rbNo.setChecked(true);
  69. int sec=sp.getInt("sec", defaultSec);
  70. etSec.setText(Integer.toString(sec));
  71. Toast.makeText(Ch7_3_SharedPref.this, "2.載入設定", Toast.LENGTH_LONG).show();
  72. }});
  73. //3.回復預設
  74. btn3.setOnClickListener(new OnClickListener(){

  75. @Override
  76. public void onClick(View v) {
  77. etFile.setText(defaultName);
  78. if(defaultIsAutoFocus)
  79. rbYes.setChecked(true);
  80. else
  81. rbNo.setChecked(true);
  82. etSec.setText(Integer.toString(defaultSec));
  83. Toast.makeText(Ch7_3_SharedPref.this, "3.回復預設", Toast.LENGTH_LONG).show();
  84. }});
  85. }
  86. }
復制代碼

最新評論



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2017-7-27 06:37 , Processed in 0.042739 second(s), 21 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

返回頂部