禁用alertDialog中的复选框项

禁用alertDialog中的复选框项

本文介绍了禁用alertDialog中的复选框项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过警报对话框让阵列选择多个选项,如果阵列已满,我想禁用选择

I am giving array to select multiple options through alert dialog I want to disable selection if array is full

达到限制后,我已经能够停止向数组添加项目,但是无法禁用复选框

I have been able to stop adding items to array after it reaches the limit but not able to disable the checkboxes

 AlertDialog.Builder builder=new AlertDialog.Builder(this);
 View view=inflater.inflate(R.layout.dialog_genre,null,false);
 builder.setView(view).setTitle("Select Genres");

 builder.setMultiChoiceItems(array, checkedGenres, new DialogInterface.OnMultiChoiceClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which, boolean isChecked) {
            if(isChecked) {
                if (!selectedgenre.contains(String.valueOf(which)))
                    if(selectedgenre.size()<5)
                {
                    selectedgenre.add(String.valueOf(which));
                    checkedGenres[which]=true;
                }
                else{
                        Toast.makeText(getApplicationContext(),"you can't add more genres",Toast.LENGTH_LONG).show();
                    }
            }
            else if (selectedgenre.contains(String.valueOf(which)))
            {
             selectedgenre.remove(String.valueOf(which));
             checkedGenres[which]=false;
        }
    }
}

推荐答案

好的,我已经解决了这个问题,两天后我才知道禁用复选框是一个漫长而忙碌的过程,但是我们可以禁止选择,这就是我想要的去做.这是我的解决方法

okay I have solved the issue, after two days I came to know disabling checkbox is lengthy and hectic process but we can prohibit the selection, and that's what I wanted to do. here is my solution

    builder.setMultiChoiceItems(array, checkedGenres, new DialogInterface.OnMultiChoiceClickListener() {
        int count = 0;
        @Override
        public void onClick(DialogInterface dialog, int which, boolean isChecked) {
            if(isChecked) {
                if (!selectedgenre.contains(String.valueOf(array[which])))
                    if(selectedgenre.size()<5)
                {
                    selectedgenre.add(String.valueOf(array[which]));
                    checkedGenres[which]=true;
                }
                else{
                        count--;
                        ((AlertDialog) dialog).getListView().setItemChecked(which, false);
                        checkedGenres[which]=false;
                        Toast.makeText(getApplicationContext(),"you can't add this genre",Toast.LENGTH_LONG).show();
                    }
            }
            else if (selectedgenre.contains(String.valueOf(array[which])))
            {
                selectedgenre.remove(String.valueOf(array[which]));
                checkedGenres[which]=false;
            }
        }
    }

这篇关于禁用alertDialog中的复选框项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 02:27