- 資料處理 : Shared Preferences - Detail : Link
- 資料設定 : SharedPreferences.Editor - Detail : Link
- 資料設定方法:
- Ex::
SharedPreferences settings = getSharedPreferences(SETTING_PREF, 0);
settings.edit()
.putString(SHARED_MSG1, mEditText01.getText().toString())
.putInt(SHARED_MSG2, int2)
.putLong(SHARED_MSG3, long3)
.putFloat(SHARED_MSG4, float4)
.putBoolean(SHARED_MSG5, boolean5)
.commit();
- 資料讀取方法
- Ex:
SharedPreferences settings = getSharedPreferences(SETTING_PREF, 0);
String msg1 = settings.getString(SHARED_MSG1, "");
int msg2_int = settings.getInt(SHARED_MSG2, defint2);
long msg3_long = settings.getLong(SHARED_MSG3, deflong3);
float msg4_float = settings.getFloat(SHARED_MSG4, deffloat4);
boolean msg5_boolean = settings.getBoolean(SHARED_MSG5, defboolean5);
範例:
練習:
- 製作SharedPreferences版的MSN-Like程式
- 請加入清空歷史訊息的效果
沒有留言:
張貼留言