Android Checkbox preference

后端 未结 3 1904
温柔的废话
温柔的废话 2020-12-23 12:11

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 toa

3条回答
  •  别那么骄傲
    2020-12-23 12:29

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

提交回复
热议问题