【问题标题】:How to set disabled status using `SharedPreferences`如何使用“SharedPreferences”设置禁用状态
【发布时间】:2015-07-03 06:10:34
【问题描述】:
我使用timer 禁用了button 2 分钟。
btn_Verify.setEnabled(false);
Log.e("LoginActivity", "counter :" + counter);
Handler h = new Handler();
h.postDelayed(new Runnable() {
@Override
public void run() {
btn_Verify.setEnabled(true);
}
},120000);
如果我退出应用程序并重新启动,button 将被启用。因此,我必须将其保存在SharedPreference 并阻止它 2 分钟然后取消阻止它。
谁能告诉我如何将其保存在SharedPreference 中。
提前致谢。
【问题讨论】:
标签:
android
sharedpreferences
【解决方案1】:
这是会话存储类。
public class SessionStore {
public static boolean button_value = true;
public static void saveBoolean(Context context, String key, boolean value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(
STOREPREFF_NAME, Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
public static boolean getBoolean(Context context, String key,
boolean defaultValue) {
SharedPreferences sharedPreferences = context.getSharedPreferences(
STOREPREFF_NAME, Context.MODE_PRIVATE);
return sharedPreferences.getBoolean(key, defaultValue);
}
然后你需要在哪里启用或禁用按钮。你可以保存它
在会话存储中。
For saving
SessionStore.saveBoolean(getApplicationContext(),
SessionStore.button_value , "true or false");
For getting
SessionStore.getBoolean(getApplicationContext(),
SessionStore.button_value , "");