我需要找到一种方法来在单击CheckboxPreference时切换返回值。我已经在settings.xml中设置了一个键。点击工作正常,我只是对每次点击时如何返回新值感到好奇。
final Preference disableRemixingPref = (Preference) findPreference("disableRemixing");
disableRemixingPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
// I don't always want to return true here. Basically, on every click I want to return the opposite of what the selection was before.
return true;
}
});
答案 0 :(得分:1)
我设法很快就弄明白了。在切换布尔值时进行了一些搜索:Cleanest way to toggle a boolean variable in Java?
final Preference disableRemixingPref = (Preference) findPreference("disableRemixing");
disableRemixingPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
boolean toggle = false;
public boolean onPreferenceClick(Preference preference) {
disableRemixingPref.setSelectable(true);
// Disable remixing by default
ParseUser currentUser = ParseUser.getCurrentUser();
currentUser.put("remixByDefault", toggle);
currentUser.saveInBackground();
toggle = !toggle;
return toggle;
}
});