如何检查 CheckBoxPreference 是否被选中?

How to check CheckBoxPreference is checked or not?

我有一个设置页面,我在其中添加了 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。你能帮我实现这个目标吗?

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

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