我想知道共享首选项中是否已经存在某个键值对。
sharedPref=getApplicationContext().getSharedPreferences("sharedf",Context.MODE_PRIVATE);
editor=sharedPref.edit();
editor.putString("secretKey",e1.getText().toString());
editor.commit();现在,我想检查secretKey是否已经存在于共享首选项中。
发布于 2015-01-22 01:10:24
这很简单。有一种方法:
public abstract boolean contains (String key)你可以在这里阅读:
http://developer.android.com/reference/android/content/SharedPreferences.html
发布于 2015-01-22 01:09:41
你可以用下一种方法来实现
sharedPref=getApplicationContext().getSharedPreferences("sharedf",Context.MODE_PRIVATE);
String secretKey = sharedPref.getString("secretKey", null);
if (null == secretKey) {
// not exists
} else {
// exists
}发布于 2015-01-22 01:18:29
if (mPrefs.contains(SETTING_THEME)) {
// read the value
mThemeSettings = mPrefs.getString(SETTING_THEME, THEME_KEY_DEFAULT);
}
// create a new "prefs entry" (first start, after installing the app)
else mEditor.putString(SETTING_THEME, mThemeSettings).apply();https://stackoverflow.com/questions/28072892
复制相似问题