禁用alertDialog中的复选框项

时间:2016-08-20 10:57:01

标签: android android-alertdialog

我给数组通过警告对话框选择多个选项我想在数组已满时禁用选择

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

 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;
        }
    }
}

enter image description here

4 个答案:

答案 0 :(得分:1)

ListView中的视图是可重用的,因此items aren't really linked to a view。但是,将视图添加到AlertDialog的ListView中的ListView时,可以启用或禁用该视图。 ViewGroup.setOnHierarchyChangeListener允许您这样做。

    final CharSequence[] items = new CharSequence[]
            {"9×19mm Sidearm", ".40 Dual Elites", "228 Compact", "Night Hawk .50C"};
    boolean[] checkedItems = new boolean[]{false, false, true, true};
    final boolean[] disabledItems = new boolean[]{false, true, false, true};

    Context context = this;
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
    dialogBuilder.setTitle("Select your pistol");

    dialogBuilder.setMultiChoiceItems(items, checkedItems,
            new DialogInterface.OnMultiChoiceClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i, boolean checked) { 

        }
    });

    AlertDialog alertDialog = dialogBuilder.create();
    alertDialog.getListView().setOnHierarchyChangeListener(
            new ViewGroup.OnHierarchyChangeListener() {
        @Override
        public void onChildViewAdded(View parent, View child) {
            CharSequence text = ((AppCompatCheckedTextView)child).getText();
            int itemIndex = Arrays.asList(items).indexOf(text);
            child.setEnabled(disabledItems[itemIndex]);
        }

        @Override
        public void onChildViewRemoved(View view, View view1) {
        }
    });

    alertDialog.show();

Outcome of code sample (Android 9, API 28)

请注意,setEnable不会避免clickListeners触发。如果您不想单击复选框,请添加child.setOnClickListener(null)

答案 1 :(得分:0)

   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{
                        //set your checkbox to false here
                        //yourCheckbox.setchecked(false); 
                            Toast.makeText(getApplicationContext(),"you can't add more genres",Toast.LENGTH_LONG).show();
                        }
                }
                else
                {
                 selectedgenre.remove(String.valueOf(which));
                 checkedGenres[which]=false;
            }
        }
    }

在你的其他部分试试这个问题

答案 2 :(得分:0)

在你的其他部分,在你的复选框上放置可点击的假,并更改颜色,以便用户可以识别它。

答案 3 :(得分:0)

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

    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;
            }
        }
    }