0

我有一个设置页面,我在其中添加了CheckBoxPreference我需要检查复选框是否被选中?我的代码是:

<PreferenceCategory android:title="Google Maps">

    <CheckBoxPreference
        android:defaultValue="true"
        android:summary="@string/markerSummary"
        android:title="@string/markerTitle"
        android:key="@string/markerKey" />

</PreferenceCategory>

在settingActivity.java

Preference DragPref = findPreference(getString(R.string.markerKey));
        DragPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                boolean checked = Boolean.valueOf(newValue.toString());
                return false;
            }
        });

此代码始终返回 false;添加上面的代码后,它不会变为 false。你能帮我如何实现这一目标吗?

4

1 回答 1

1

使用这个,希望它能解决你的问题。

Preference DragPref = findPreference(getString(R.string.markerKey));
    DragPref.setOnPreferenceChangeListener(new  Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            boolean checked = (Boolean) newValue;
            return true; 
           // Note: return true to update the state of the Preference with the new value. If you want to disallow the change return false
        }
    });
于 2019-03-07T10:04:28.953 回答