如何限制选中复选框的数量 android

how to limit number of checked checkBoxes android

如何限制 android 中选中复选框的数量?我以编程方式添加了多个复选框,很难跟踪它们。

这是用于添加它们的代码:

                        final CheckBox currentVariantCheckbox = new CheckBox(getApplicationContext());
                        checkBoxGroupList.add(currentVariantCheckbox);
                        Log.d(TAG, "onDataChange: added " + currentVariantCheckbox + " to the checkboxgrouplist; size = " + checkBoxGroupList.size());
                        currentVariantCheckbox.setChecked((Boolean) currentVariant.child("checked").getValue());
                        LinearLayout checkboxGroupLayout = new LinearLayout(getApplicationContext());
                        checkboxGroupLayout.setOrientation(LinearLayout.HORIZONTAL);

                        currentVariantCheckbox.setText(currentVariant.child("name").getValue(String.class));
                        TextView currentVariantPriceTag = new TextView(getApplicationContext());
                        checkboxGroupLayout.addView(currentVariantCheckbox);
                        if (currentVariant.child("price").exists()) {
                            currentVariantPriceTag.setText("+" + currentVariant.child("price").getValue(float.class).toString() + " €");
                            checkboxGroupLayout.addView(currentVariantPriceTag);

好的,所以我没有使用 onCheckedStateChangedLister,而是使用了 OnClickListener。我创建了一个 ArrayList 来跟踪所有选中的复选框:

final ArrayList<CheckBox> checkedList = new ArrayList<>();//this is the list to keep track of checked checkboxes
int maxOptions = 3
int minOptions = 1
currentVariantCheckbox.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Boolean currentCheckState = currentVariantCheckbox.isChecked();
                                if (currentCheckState) {//if the clicked checkboxes was unchecked and is now checked
                                    checkedList.add(currentVariantCheckbox);
                                    Log.d(TAG, "onClick: added " + currentVariantCheckbox + " ;checkedList.size is now: " + checkedList.size());
                                    if (checkedList.size() >= maxOptions) {
                                        checkedList.get(0).setChecked(false);
                                        checkedList.remove(0);// if limit exceeded remove the first element from the checked list
                                        Log.d(TAG, "onCheckedChanged: checkedList's size is now: " + checkedList.size());
                                    }
                                } else if (checkedList.size() <= minOptions) {
                                    currentVariantCheckbox.setChecked(true);
// if the list is empty just add the clicked checkbox to the list for example here 0
// and it will be checked automatically
                                } else {
                                    checkedList.remove(currentVariantCheckbox);
// if the checkbox was already checked and no limit is exceeded
// then it will be unchecked therfore it should be removed from checkedList
                                }
                            }
                        });