0

单击我的 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;
            }

        });
4

1 回答 1

1

我很快就自己弄清楚了。是否对切换布尔值进行了一些搜索:在 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;
        }

    });
于 2015-11-15T01:11:54.487 回答