如何检查CheckBoxPreference是否被选中?

时间:2019-03-07 08:45:33

标签: android checkboxpreference

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

1 个答案:

答案 0 :(得分: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
        }
    });
相关问题