【问题标题】:check if all check boxes are checked. android检查是否选中了所有复选框。安卓
【发布时间】:2020-03-05 20:25:17
【问题描述】:

我正在尝试处理一个事件,如果该人选中所有复选框,则应该发生一个动作,即使按钮可点击。

到目前为止,我只能检查一个框是选中还是未选中,但找不到一种方法来设置一个计数器来计算单击了多少个复选框。

复选框将是动态的,但在此代码示例中,我只使用了静态值。

我尝试设置一个计数器,如果我单击一个复选框,它应该会增加,但找不到正确的方法。

//我调用的方法

  ArrayList<String> arrayList= new ArrayList<>();
            arrayList.add("2Pac");
            arrayList.add("Biggie");
            arrayList.add("Nas");
            // Create Checkbox Dynamically
            for(int i=0;i<arrayList.size();i++) {

                CheckBox checkBox = new CheckBox(getContext());
                checkBox.setText(arrayList.get(i));
                checkBox.setTextSize(20);
                checkBox.setId(++checkBoxID);
                checkBox.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                   // int counter = -1;
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
                    {
                       // String msg = "You have " + (isChecked ? "checked" : "unchecked") + " this box it Checkbox.";
                        //Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
                        if(checkBox.isChecked())
                        {
                            //Toast.makeText(getActivity(), ""+ counter, Toast.LENGTH_SHORT).show();
                        }else
                            {
                          //      counter++;
                             //   Toast.makeText(getActivity(), ""+ counter, Toast.LENGTH_SHORT).show();
                            }
                    }
                });

                for(int j=0;j<allCheckBox.size();j++)
                {
                    if(allCheckBox.size()==arrayList.size())
                    {
                        //Toast.makeText(MainActivity.this, ""+ checkBox.getId(), Toast.LENGTH_SHORT).show();
                    }
                }
                // Add Checkbox to LinearLayout
                if (linearLayout != null) {
                    linearLayout.addView(checkBox);
                }

到目前为止,我已经花了一天多的时间试图弄清楚这个! 非常感谢任何帮助或想法。

【问题讨论】:

    标签: java android checkbox


    【解决方案1】:

    在 for 循环外定义 counter,初始值为 0,当用户选中复选框时,将其加一,当用户取消选中复选框时,将计数器减一并保持相同的 onCheckedChanged 侦听器检查此类复选框的数量

     ArrayList<String> arrayList= new ArrayList<>();
     arrayList.add("2Pac");
     arrayList.add("Biggie");
     arrayList.add("Nas");
    
     int counter = 0; //all checkboxes unchecked
     // Create Checkbox Dynamically
     for(int i=0;i<arrayList.size();i++) {
    
         CheckBox checkBox = new CheckBox(getContext());
         checkBox.setText(arrayList.get(i));
         checkBox.setTextSize(20);
         checkBox.setId(++checkBoxID);
         checkBox.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    
         checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
                        {
    
                            if(checkBox.isChecked())
                            {
                               counter++;
                               if (counter == arrayList.size()) {
                                  //make your button clickable here
                               }
                            } else {
                               counter--;
                               //disable your button incase the user uncheck one of the checkboxes after all checkboxes were checked
                            } //end if
                        }
         });
    
    
         // Add Checkbox to LinearLayout
         if (linearLayout != null) {
             linearLayout.addView(checkBox);
         } //end if
     } //end for
    

    我用手机写了代码,所以我没有测试代码 我希望它有效

    【讨论】:

    • 这确实有效,但我必须像这样制作计数器:final int[] counter = {0};
    • 谢谢伙计。这太简单了,我自己也搞不明白:D
    • 很高兴它帮助你,快乐编码
    猜你喜欢
    • 2018-03-27
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    • 2013-10-28
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多