简体   繁体   中英

Disable Checkbox click in multiselectListPreference in android

I am using mutliselectlistpreference that extends DialogPreference in my application. And i have not use any adapter for building the UI. Please find the below image. 在此处输入图片说明

The issue here is I am able to persist the CheckBox checked for Monday and Tuesday but i am not able to make the items read only wherein user will not able to unchecked the items. I want to make both items grey out. could you please help me out on this ?

 @Override
    protected void onPrepareDialogBuilder(Builder builder) {
        super.onPrepareDialogBuilder(builder);

        if (entries == null || entryValues == null) {
            throw new IllegalStateException(
                    "MultiSelectListPreference requires an entries array and an entryValues array.");
        }

        checked = new boolean[entryValues.length];
        List<CharSequence> entryValuesList = Arrays.asList(entryValues);
        List<CharSequence> entriesList = Arrays.asList(entries);
        for (int i = 0; i < entryValues.length; ++i) {
            if("Monday".equals(entriesList.get(i).toString())){
                checked[i]=true;
            }
        }
        if (values != null) {
            for (String value : values) {
                int index = entryValuesList.indexOf(value);
                if (index != -1) {
                    checked[index] = true;
                }
            }
        }


        builder.setMultiChoiceItems(entries, checked,
                new OnMultiChoiceClickListener() {
                    public void onClick(DialogInterface dialog, int which,
                            boolean isChecked) {
                        checked[which] = isChecked;
                    }
                });

    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);

        if (positiveResult && entryValues != null) {
            for (int i = 0; i < entryValues.length; ++i) {

                if (checked[i]) {

                    newValues.add(entryValues[i].toString());

                }
            }

            if (callChangeListener(newValues)) {
                setValues(newValues);
            }
        }
    }

    @Override
    protected Object onGetDefaultValue(TypedArray a, int index) {
        CharSequence[] array = a.getTextArray(index);

        Set<String> set = new HashSet<String>();

        for (CharSequence item : array) {
            set.add(item.toString());
        }

        return set;
    }

    @Override
    protected void onSetInitialValue(boolean restorePersistedValue,
            Object defaultValue) {
        @SuppressWarnings("unchecked")
        Set<String> defaultValues = (Set<String>) defaultValue;

        setValues((restorePersistedValue ? getPersistedStringSet(values)
                : defaultValues));
    }

    private Set<String> getPersistedStringSet(Set<String> defaultReturnValue) {
        String key = getKey();
        //String value = getSharedPreferences().getString("4", "Generic");
        return getSharedPreferences().getStringSet(key, defaultReturnValue);
    }

    private boolean persistStringSet(Set<String> values) {
        if (shouldPersist()) {
            // Shouldn't store null
            if (values == getPersistedStringSet(null)) {

                return true;
            }
        }

        SharedPreferences.Editor editor = getEditor();
        editor.putStringSet(getKey(), values);
        editor.apply();
        return true;
    }

    @Override
    protected Parcelable onSaveInstanceState() {
        if (isPersistent()) {
            return super.onSaveInstanceState();
        } else {
            throw new IllegalStateException("Must always be persistent");
        }
    }

You can use them;

checkBox.setEnabled(true); // enable checkbox

checkBox.setEnabled(false); // disable checkbox

Also you can disable them after check the checkBox with this code:

checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){

    @Override
    public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {                   
      if (isChecked){
          checkBox.setEnabled(false); // disable checkbox 
      }
    }    
});

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