为 CheckboxPreference onClick 切换 return 值
Toggle return value for CheckboxPreference onClick
我需要找到一种方法来在单击我的 CheckboxPreference 时切换 return 值。我已经在 settings.xml 中设置了一个密钥。点击效果很好,我只是想知道如何在每次点击时 return 一个新值。
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;
}
});
我自己很快就弄明白了。对切换布尔值进行了一些搜索: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;
}
});
我需要找到一种方法来在单击我的 CheckboxPreference 时切换 return 值。我已经在 settings.xml 中设置了一个密钥。点击效果很好,我只是想知道如何在每次点击时 return 一个新值。
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;
}
});
我自己很快就弄明白了。对切换布尔值进行了一些搜索: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;
}
});