1

我想知道如何在代码中使用MultiSelectListPreference首选项。

以下代码取自 API 示例文件 PreferencesFromCode.java,如果有人可以为 MultiSelectListPreference 提供类似的示例,您会很高兴:)

// List preference
ListPreference listPref = new ListPreference(this);
listPref.setEntries(R.array.entries_list_preference);
listPref.setEntryValues(R.array.entryvalues_list_preference);
listPref.setDialogTitle(R.string.dialog_title_list_preference);
listPref.setKey("list_preference");
listPref.setTitle(R.string.title_list_preference);
listPref.setSummary(R.string.summary_list_preference);
dialogBasedPrefCat.addPreference(listPref);
4

1 回答 1

4

我在网上找不到任何例子,但我把它放在一起,它可以工作。

编辑:此解决方案仅适用于 ICS+。Honeycomb 完全忽略 .setValues() 并且传递给侦听器的 HashSet 包含不同的值。这是一个已知的错误,但我希望这能帮助想要在 Android v4+ 中实现的人

    MultiSelectListPreference listPreference = new MultiSelectListPreference(context);
    listPreference.setTitle(R.string.configure_category_title);
    listPreference.setDialogTitle(R.string.configure_category_title);
    listPreference.setSummary(R.string.configure_category_summary);
    listPreference.setEntries(R.array.configure_category_array);
    listPreference.setEntryValues(new CharSequence[]{
            ProcessList.PREF_SERVICES + mAppWidgetId,
            ProcessList.PREF_INACTIVE + mAppWidgetId,
            ProcessList.PREF_INTERNAL + mAppWidgetId
    });

    //Create a Set<String> with list items that should be selected
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
    boolean showServices = sharedPref.getBoolean(ProcessList.PREF_SERVICES + mAppWidgetId, true);
    boolean showInactive = sharedPref.getBoolean(ProcessList.PREF_INACTIVE + mAppWidgetId, true);
    boolean showInternal = sharedPref.getBoolean(ProcessList.PREF_INTERNAL + mAppWidgetId, true);

    String[] strings = new String[3];
    int cnt = 0;
    if (showServices)
        strings[cnt++] = ProcessList.PREF_SERVICES + mAppWidgetId;
    if (showInactive)
        strings[cnt++] = ProcessList.PREF_INACTIVE + mAppWidgetId;
    if (showInternal)
        strings[cnt] = ProcessList.PREF_INTERNAL + mAppWidgetId;

    Set<String> mySet = new HashSet<String>();
    Collections.addAll(mySet, strings);

    //Add the set
    listPreference.setValues(mySet);

    //Listen for changes, I'm not sure if this is how it's meant to work, but it does :/
    listPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        public boolean onPreferenceChange(Preference preference, Object o) {

            HashSet hashSet = (HashSet) o;
            Iterator stringIterator = hashSet.iterator();
            boolean[] states = {false, false, false};
            String prefString;

            while (stringIterator.hasNext()) {

                prefString = (String) stringIterator.next();

                if (prefString == null)
                    continue;

                if (prefString.compareTo(ProcessList.PREF_SERVICES + mAppWidgetId) == 0)
                    states[0] = true;
                else if (prefString.compareTo(ProcessList.PREF_INACTIVE + mAppWidgetId) == 0)
                    states[1] = true;
                else if (prefString.compareTo(ProcessList.PREF_INTERNAL + mAppWidgetId) == 0)
                    states[2] = true;

            }

            PreferenceManager
                    .getDefaultSharedPreferences(getActivity())
                    .edit()
                    .putBoolean(ProcessList.PREF_SERVICES + mAppWidgetId, states[0])
                    .putBoolean(ProcessList.PREF_INACTIVE + mAppWidgetId, states[1])
                    .putBoolean(ProcessList.PREF_INTERNAL + mAppWidgetId, states[2])
                    .commit();

            return true;
        }
    });

    preferenceCategory.addPreference(listPreference);
于 2012-12-10T13:47:20.747 回答