如何在相应的 XML 文件中明确定义单个Android共享首选项的默认值? E.g:
<CheckBoxPreference
android:defaultValue="false"
android:key="fulldb"
android:summary="No selection rules apply"
android:title="Use Full Database" />
答案 0 :(得分:1)
喜欢这个..
public class Calc extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
@Override
protected void onCreate(Bundle state){
super.onCreate(state);
. . .
// Restore preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean silent = settings.getBoolean("silentMode", false); //default value if nothing is in the preference is the last parameter false.
setSilent(silent);
}
@Override
protected void onStop(){
super.onStop();
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("silentMode", mSilentMode);
// Commit the edits!
editor.commit();
}
}
答案 1 :(得分:0)
见下文: 存储价值:
public static SharedPreferences myPrefs;
public static SharedPreferences.Editor prefsEditor;
myPrefs = this.getSharedPreferences("myPrefs",MODE_WORLD_WRITEABLE);
prefsEditor = myPrefs.edit();
prefsEditor.putBoolean("FullResultIsOn", true); // value to store
prefsEditor.commit();
现在检索值:
myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
fullResultSound = myPrefs.getBoolean("FullResultIsOn", false);
享受。 :) 感谢。