在首选项活动中更改CheckBoxPreference

时间:2014-05-04 09:47:53

标签: android application-settings

我需要在Preference活动中更改Check Box Preference,但它似乎不起作用:

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;
            }
        };

    }

}

3 个答案:

答案 0 :(得分:2)

这应该有效:

final CheckBoxPreference autoP = (CheckBoxPreference) getPreferenceManager().findPreference("autoP");

autoP.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {            
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        if ((Boolean)newValue == true) {
            getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }else {
            getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }
        return true;
    }
}); 

答案 1 :(得分:0)

试试这个:

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(new OnPreferenceChangeListener() {
                @Override
                public boolean onPreferenceChange(Preference preference,Object newValue) {
                    updateOnPreferenceChange();
                }
            });

        }

        private void updateOnPreferenceChange() {

            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
            String pref_name = "should_be_pref_name_in_xml_file";
            boolean checked = sharedPreferences.getBoolean(pref_name, defaultValue);
            if (checked) {
                // todo
            } else {
                // todo
            }
        }
    }
}

答案 2 :(得分:0)

试试这个

public class SettingsActivity extends PreferenceFragment  {


@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.prefs);

}

@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
        Preference preference) {


    String key = preference.getKey();

    if (key.equals("key")) {
        if (((CheckBoxPreference) preference).isChecked()){
            // do things if the checkbox is checked       
             getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  } else {
      // do things if it is unchecked
        getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
     }
return true;
   }
}