不调用MultiSelectPreference onPreferenceChange回调侦听器

时间:2017-08-07 06:34:48

标签: java android xml android-preferences

我正在尝试制作MultiSelectPreference。但是,当单击列表中的项目时,列表永远不会收到回调侦听器。

XML代码:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layoutDirection="locale">

    <PreferenceCategory android:title="@string/preference_display_category">

        <MultiSelectListPreference
            android:key="preference_filter_entries"
            android:summary="@string/preference_filter_entries_summary"
            android:title="@string/preference_filter_entries_title"
            android:entries="@array/preference_entry_array"
            android:entryValues="@array/preference_filter_entries_array_values"
          android:dialogTitle="@string/preference_filter_entries_dialog_title"/>

    </PreferenceCategory>

</PreferenceScreen> 

Java代码:

MultiSelectListPreference multiSelectListPreference;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.fragment_preferences);
         getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        multiSelectListPreference = (MultiSelectListPreference) findPreference("preference_filter_entries");

        multiSelectListPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
Timber.d("onPreferenceChange called %s", "1");
                return false;
            }
        });
    }

在检查列表中的任何项目时,从未调用上面的Timber日志!

1 个答案:

答案 0 :(得分:0)

将回报更改为true,如下所示,

multiSelectListPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
Timber.d("onPreferenceChange called %s", "1");
                return true;
            }
        });