I have checkbox in preferences. I want to keep screen on if checkbox is checked and not to keep screen on if checkbox is not checked.
I want something like this:
boolean keepScreen = sharedPrefs.getBoolean("chck_screen", false);
if (keepScreen.equals(false)) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
Please, help.
If you are using a PreferenceActivity and have the checkbox declared in XML with the key "checkbox_preference" (rename it to whatever you have) you can do this:
CheckBoxPreference pref = (CheckBoxPreference) findPreference("checkbox_preference");
pref.isChecked(); returns if it is checked or not
Additionally, you can set a listener for whenever the value is changed
pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue)
{
boolean checked = Boolean.valueOf(newValue.toString());
//set your shared preference value equal to checked
return true;
}
});
您还可以使用PreferenceManager.getSharedPreferences().getBoolean(String key, boolean defValue)
来获取首选项值
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.