2012年4月20日 星期五

[Android]SharedPreferences 偏好設定使用範例


【程式開始執行時畫面】

【輸入欄位資料時畫面】

【讀出 SharedPreferences 資料時畫面】


【清除螢幕後畫面】
<EditGetSharedPreferences.java>
   1: package com.android.editgetsharedpreferences;
   2:  
   3: import java.text.SimpleDateFormat;
   4: import java.util.Date;
   5:  
   6: import android.app.Activity;
   7: import android.content.Context;
   8: import android.content.SharedPreferences;
   9: import android.os.Bundle;
  10: import android.view.View;
  11: import android.view.inputmethod.InputMethodManager;
  12: import android.widget.Button;
  13: import android.widget.EditText;
  14: import android.widget.TextView;
  15:  
  16: public class EditGetSharedPreferences extends Activity {
  17:  
  18:     public static final String PREF = "EditGetPreferences";
  19:     public static final String PREF_ACCOUNT = "Account";
  20:     public static final String PREF_PASSORD = "Password";
  21:     public static final String PREF_UPDATETIME = "UpdateTime";
  22:     SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MMM-dd hh:mm:ss");
  23:         
  24:     private EditText edittextAccount, edittextPassword;
  25:     private TextView textviewAccount, textviewPassword, textviewUpdateTime;
  26:     private Button btnSave, btnGet, btnExit, btnClearScreen, btnClearPref;
  27:     
  28:     
  29:     @Override
  30:     public void onCreate(Bundle savedInstanceState) {
  31:         super.onCreate(savedInstanceState);
  32:         setContentView(R.layout.main);
  33:         findViews();
  34:         setButtonListeners();
  35:     }
  36:     
  37:     private void findViews() {
  38:         edittextAccount = (EditText) findViewById(R.id.accountEditText);
  39:         edittextPassword = (EditText) findViewById(R.id.passwordEditText);
  40:         textviewAccount = (TextView) findViewById(R.id.accountTextView);
  41:         textviewPassword = (TextView) findViewById(R.id.passwordTextView);
  42:         textviewUpdateTime = (TextView) findViewById(R.id.updatetimeTextView);
  43:         btnSave = (Button) findViewById(R.id.saveBtn);
  44:         btnClearPref = (Button) findViewById(R.id.clearprefBtn);
  45:         btnClearScreen = (Button) findViewById(R.id.clearscreenBtn);
  46:         btnExit = (Button) findViewById(R.id.exitBtn);
  47:         btnGet = (Button) findViewById(R.id.getBtn);
  48:         
  49:     }
  50:     
  51:     private void setButtonListeners(){
  52:         btnSave.setOnClickListener(new Button.OnClickListener(){
  53:             public void onClick(View v) {
  54:                 // 儲存資料至 SharedPreferences
  55:                 saveSharedPreferences();
  56:                 // set Focus On edittextAccount
  57:                 edittextAccount.requestFocus();
  58:             } 
  59:         });
  60:  
  61:         btnClearPref.setOnClickListener(new Button.OnClickListener(){
  62:             public void onClick(View v) {
  63:                 // 清除 SharedPreferences 資料
  64:                 clearSharedPreferences();
  65:                 // set Focus On edittextAccount
  66:                 edittextAccount.requestFocus();
  67:             } 
  68:         });
  69:         
  70:         btnClearScreen.setOnClickListener(new Button.OnClickListener(){
  71:             public void onClick(View v) {
  72:                 // 清除所有 EditTexts 的資料
  73:                 clearEditTexts();
  74:                 // 清除所有 TextViews 的資料
  75:                 clearTextViews();
  76:                 // set Focus On edittextAccount
  77:                 edittextAccount.requestFocus();
  78:             } 
  79:         });
  80:         
  81:         btnExit.setOnClickListener(new Button.OnClickListener(){
  82:             public void onClick(View v) {
  83:                 // 離開程式
  84:                 android.os.Process.killProcess(android.os.Process.myPid());
  85:             } 
  86:         });
  87:         
  88:         btnGet.setOnClickListener(new Button.OnClickListener(){
  89:             public void onClick(View v) {
  90:                 // 讀取 SharedPreferences 資料
  91:                 getSharedPreferences();
  92:                 // set Focus On edittextAccount
  93:                 edittextAccount.requestFocus();
  94:             } 
  95:         });
  96:     }
  97:     
  98:     private void saveSharedPreferences() {
  99:         // 儲存資料至 SharedPreferences
 100:         Date date = new Date (System.currentTimeMillis());
 101:         SharedPreferences settings = getSharedPreferences("PREF", 0);
 102:         SharedPreferences.Editor preEdit = settings.edit();
 103:         preEdit.putString("PREF_ACCOUNT", edittextAccount.getText().toString());
 104:         preEdit.putString("PREF_PASSWORD", edittextPassword.getText().toString());
 105:         preEdit.putString("PREF_UPDATETIME", date.toString());
 106:         preEdit.commit();
 107:         // 隱藏 SoftKeyboard
 108:         hideSoftKeyboard();
 109:         // 清除所有 TextViews 的資料
 110:         clearTextViews();
 111:     }
 112:     
 113:     private void getSharedPreferences() {
 114:         // 讀取 SharedPreferences 資料
 115:         SharedPreferences settings = getSharedPreferences("PREF", 0);
 116:         textviewAccount.setText(settings.getString("PREF_ACCOUNT", ""));
 117:         textviewPassword.setText(settings.getString("PREF_PASSWORD", ""));
 118:         textviewUpdateTime.setText(settings.getString("PREF_UPDATETIME", ""));
 119:         // 隱藏 SoftKeyboard
 120:         hideSoftKeyboard();
 121:     }
 122:  
 123:     private void clearSharedPreferences() {
 124:         // 清除 SharedPreferences 資料
 125:         SharedPreferences settings = getSharedPreferences("PREF", 0);
 126:         settings.edit().clear().commit();
 127:         // 清除所有 TextViews 的資料
 128:         clearTextViews();
 129:     }
 130:     
 131:     private void clearEditTexts(){
 132:         // 清除所有 EditTexts 的資料
 133:         edittextAccount.setText("");
 134:         edittextPassword.setText("");
 135:     }
 136:     
 137:     private void clearTextViews(){
 138:         // 清除所有 TextViews 的資料
 139:         textviewAccount.setText("");
 140:         textviewPassword.setText("");
 141:         textviewUpdateTime.setText("");
 142:     }
 143:     
 144:     private void hideSoftKeyboard(){
 145:         // 隱藏 SoftKeyboard
 146:         InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
 147:         imm.hideSoftInputFromWindow(getWindow().peekDecorView().getWindowToken(), 0);
 148:     }
 149: }

<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:     <LinearLayout
   8:         android:id="@+id/linearLayout1"
   9:         android:layout_width="match_parent"
  10:         android:layout_height="wrap_content"
  11:         android:orientation="vertical" >
  12:  
  13:         <EditText
  14:             android:id="@+id/accountEditText"
  15:             android:layout_width="match_parent"
  16:             android:layout_height="wrap_content"
  17:             android:hint="@string/accountHint" >
  18:  
  19:             <requestFocus />
  20:  
  21:         </EditText>
  22:  
  23:         <EditText
  24:             android:id="@+id/passwordEditText"
  25:             android:layout_width="match_parent"
  26:             android:layout_height="wrap_content"
  27:             android:hint="@string/passwordHint"
  28:             android:inputType="textPassword" >
  29:  
  30:         </EditText>
  31:  
  32:         <LinearLayout
  33:             android:id="@+id/linearLayout2"
  34:             android:layout_width="match_parent"
  35:             android:layout_height="wrap_content" >
  36:  
  37:             <Button
  38:                 android:id="@+id/saveBtn"
  39:                 android:layout_width="wrap_content"
  40:                 android:layout_height="wrap_content"
  41:                 android:text="@string/strSave" />
  42:  
  43:             <Button
  44:                 android:id="@+id/clearscreenBtn"
  45:                 android:layout_width="wrap_content"
  46:                 android:layout_height="wrap_content"
  47:                 android:text="@string/strClearScreen" />
  48:  
  49:             <Button
  50:                 android:id="@+id/clearprefBtn"
  51:                 android:layout_width="wrap_content"
  52:                 android:layout_height="wrap_content"
  53:                 android:text="@string/strClearPref" />
  54:  
  55:             <Button
  56:                 android:id="@+id/exitBtn"
  57:                 android:layout_width="wrap_content"
  58:                 android:layout_height="wrap_content"
  59:                 android:text="@string/strExit" />
  60:  
  61:         </LinearLayout>
  62:  
  63:     </LinearLayout>
  64:  
  65:     <LinearLayout
  66:         android:id="@+id/linearLayout3"
  67:         android:layout_width="match_parent"
  68:         android:layout_height="wrap_content"
  69:         android:orientation="vertical" >
  70:  
  71:         <Button
  72:             android:id="@+id/getBtn"
  73:             android:layout_width="match_parent"
  74:             android:layout_height="wrap_content"
  75:             android:text="@string/strGet" />
  76:  
  77:         <TextView
  78:             android:id="@+id/accountTextView"
  79:             android:layout_width="match_parent"
  80:             android:layout_height="wrap_content"
  81:             android:text="@string/strNull" />
  82:  
  83:         <TextView
  84:             android:id="@+id/passwordTextView"
  85:             android:layout_width="match_parent"
  86:             android:layout_height="wrap_content"
  87:             android:text="@string/strNull" />
  88:  
  89:         <TextView
  90:             android:id="@+id/updatetimeTextView"
  91:             android:layout_width="match_parent"
  92:             android:layout_height="wrap_content"
  93:             android:text="@string/strNull" />
  94:  
  95:         <TextView
  96:             android:id="@+id/textView1"
  97:             android:layout_width="match_parent"
  98:             android:layout_height="wrap_content"
  99:             android:gravity="center"
 100:             android:text="@string/strCopyright" />
 101:  
 102:     </LinearLayout>
 103:  
 104: </LinearLayout>

<strings.xml>
   1: <?xml version="1.0" encoding="utf-8"?>
   2: <resources>
   3:  
   4:     <string name="hello">Hello World, EditGetSharedPreferences!</string>
   5:     <string name="app_name">偏好設定</string>
   6:     <string name="accountHint">請輸入帳號名稱</string>
   7:     <string name="passwordHint">請輸入密碼</string>
   8:     <string name="strSave">儲存</string>
   9:     <string name="strExit">離開</string>
  10:     <string name="strGet">讀出 SharedPreferences 資料</string>
  11:     <string name="strNull"></string>
  12:     <string name="strClearScreen">清除畫面</string>
  13:     <string name="strClearPref">清除 Pref 資料</string>
  14:     <string name="strCopyright">\nCopyright : Terry Wu (terry.wuteyu@gmail.com)</string>
  15:  
  16: </resources>

沒有留言:

張貼留言