AlertDialog MultiChoiceItems - 一次只选择2个项目

时间:2014-07-05 12:23:22

标签: android alertdialog android-alertdialog

我的警报对话框中有4个复选框:A,B,C和D.我试图获得制作它的行为,以便A只能用B,C或D选择,所以有' s一次只选择2个。

是否有解决方案能够实现此行为?我看过我能通过谷歌找到什么,但我找不到任何东西。我想我将需要使用共享首选项,然后一旦点击B,C和D中的每一个,我可以在SP中存储一个字符串,引用每个B,C,D,然后它可以检查SP中的字符串每次单击一个,如果存在从B,C或D中存储的值之一,则取消选中已勾选的那个复选框(希望这是有意义的),还是可以有更好的解决方案?感谢

到目前为止我所拥有的:

  

boolean [] checked = new boolean [] {true,false,false,false   }; //这是在我的班级顶部定义的,因此一旦对话框首次出现,默认情况下会选择A

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.filteroptions, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.filter:

        AlertDialog dialog;

        final CharSequence[] items = { "A", "B", "C", "D" };
        // arraylist to keep the selected items
        final ArrayList<Integer> seletedItems = new ArrayList<Integer>();

        shfObject = getActivity().getSharedPreferences("NAME",
                Context.MODE_PRIVATE);
        final SharedPreferences.Editor shfEditorObject = shfObject.edit();
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Filter");



        builder.setMultiChoiceItems(items, checked,
                new DialogInterface.OnMultiChoiceClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog,
                            int indexSelected, boolean isChecked) {

                        if (isChecked) {
                            seletedItems.add(indexSelected);

                            switch (indexSelected) {

                            case 0:
                                shfEditorObject.putString(
                                        "checkbox1Ticked", "Ticked1");

                                shfEditorObject.commit();
                                break;

                            case 1:

                                shfEditorObject.putString(
                                        "checkbox2Ticked", "Ticked2");

                                shfEditorObject.commit();

                                break;

                            case 2:

                                shfEditorObject.putString(
                                        "checkbox3Ticked", "Ticked3");

                                shfEditorObject.commit();

                                break;

                            case 3:

                                shfEditorObject.putString(
                                        "checkbox4Ticked", "Ticked4");

                                shfEditorObject.commit();

                                break;

                            }

                        }

                        else if (seletedItems.contains(indexSelected)) {
                            // Else, if the item is already in the
                            // array, remove it
                            // write your code when user Uchecked the
                            // checkbox

                        }

                    }
                })
                // Set the action buttons
                .setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                // Your code when user clicked on OK
                                // You can write the code to save the
                                // selected item here

                            }
                        })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                // Your code when user clicked on Cancel

                            }
                        });

        dialog = builder.create();// AlertDialog dialog; create like this
                                    // outside onClick
        dialog.show();

        return true;

    default:
        break;
    }

    return true;
}

2 个答案:

答案 0 :(得分:1)

检查每个项目时应更新计数器。当它达到最大限制时,您可以禁用其他复选框。

这个问题很好地回答了这个问题 - How to disable checkbox when a limit is reached in android?

答案 1 :(得分:0)

if(Count == 2){
            cb.setChecked(false);
        }else if(b){
            Count++;
        }else if(!b){
            Count--;
        }

你可以使用上面的逻辑。让count = 0初始 然后对项目进行计数增量。