简体   繁体   中英

PreferenceActivity MultiSelectListPreference uncheck all

When first time I'm opening MultiSelectListPreference or when uncheck all check boxes and reopen MultiSelectListPreference , all items are displaying as checked.

I need to uncheck all check boxes on above situations.

How can I solve this?

settings.xml

   <MultiSelectListPreference
        android:entries="@array/pref_color_list_titles"
        android:entryValues="@array/pref_color_list_values"
        android:key="prefColor"
        android:summary="@string/pref_color_summary"
        android:title="@string/pref_color_title" >
    </MultiSelectListPreference>

strings.xml

    <string-array name="pref_color_list_titles">
        <item>Red</item>
        <item>Green</item>
        <item>Blue</item>
    </string-array>

    <string-array name="pref_color_list_values">
        <item>r</item>
        <item>g</item>
        <item>b</item>
    </string-array>

SettingsActivity.java

public class SettingsActivity extends PreferenceActivity {

    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.settings);    
    }    
}

You need just to pass empty Set to MultiSelectListPreference

Set<String> uncheckValues = new HashSet<>();
myMultiSelectListPreference.setValues(uncheckValues);

If you want check all values, you can do this one

String[] defaultValuesAsArray = getResources().getStringArray(R.array.default_values_multiselectlist_pref);
Set<String> defaultValues = new HashSet<>(Arrays.asList(defaultValuesAsArray)); //convert array to set
myMultiSelectListPreference.setValues(defaultValues);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM