Android Checkbox preference

六月ゝ 毕业季﹏ 提交于 2019-11-28 16:19:09

问题


I cannot find any tutorials on checkbox preference. I can use a listpreference, but I can't use checkbox preference. For now, I want that if user sets on the checbox, a toast msg says "true" and if he sets it off, the toast msg says "false". So far I have this:

preferences.xml:

 <CheckBoxPreference
          android:title="Show Call UI"
          android:defaultValue="true"
          android:summary="Show Call Interface when clicking call button"
          android:key="checkboxPref" />

EditPreferences.java:

public class EditPreferences extends PreferenceActivity {

    String listPreference;
    boolean checkboxPreference;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }

    public void onStart(Intent intent, int startId) {
        getPrefs();
    }

    private void getPrefs() {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(getBaseContext());
        listPreference = prefs.getString("listPref", "nr1");
        checkboxPreference = prefs.getBoolean("checkboxPref", true);
    }
}

Edit: Solution thanks to David Caunt:

checkboxPreference.
    setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

    public boolean onPreferenceChange(Preference preference, Object newValue) {
        if (newValue.toString().equals("true")) {
            Toast.makeText(getApplicationContext(), "CB: " + "true",
                                                    Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getApplicationContext(), "CB: " + "false",
                                                    Toast.LENGTH_SHORT).show();
        }
        return true;
    }
});

回答1:


You need to add a listener to the Preference in your onCreate method

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

    checkboxPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {            
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            Log.d("MyApp", "Pref " + preference.getKey() + " changed to " + newValue.toString());       
            return true;
        }
    }); 



回答2:


You can cast the value of the checkbox into a boolean. This might be safer and more extensible than checking the toString() value.

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

checkboxPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {            
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        if(newValue instanceof Boolean){
            Boolean boolVal = (Boolean)newValue;
        }
        return true;
    }
}); 


来源:https://stackoverflow.com/questions/6496450/android-checkbox-preference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!