我需要改變檢查偏好活動箱內偏好,但它不似乎工作:更改CheckBoxPreference偏好活動中
public class SettingsActivity extends PreferenceActivity {
static CheckBoxPreference autoP;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public static class MyPreferenceFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener{
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
autoP = (CheckBoxPreference)findPreference("autoP");
autoP.setOnPreferenceChangeListener(autoP_listener);
OnPreferenceChangeListener autoP_listener = new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference,Object newValue) {
autoP.setDefaultValue((String) newValue);
if (newValue.toString().equals("true"))
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
else
getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
return false;
}
};
}
}