复选框:选中并取消选中5中的复选框

时间:2014-05-30 12:37:08

标签: java android checkbox

我正在创建一个Android应用程序,其中我已经使用了15 Checkboxes

现在,我想检查15 Checkboxes中的5 Checkboxes。 我正在这样做,我得到5个复选框,之后它不允许检查第6个checkbox

我遇到的问题是 -

当我勾选5个复选框时,它不会被编辑。我的意思是,如果我选中2个复选框,然后取消选中它,它将保留该值。 例如 - 如果我选中4并取消选中1,则不允许我选中第5个复选框。

我想解决检查和取消选中的问题。

我的代码:

    public class MainActivity extends Activity implements OnCheckedChangeListener {

        CheckBox Veg, Nonveg, Bevarges, SoftDrink, HardDrink, FastFood,
                NorthIndian, SouthIndain, Dessert, SeaFood, Italian, Chiness,
                Maxican, Spanish, Thai;
        Button save;
        String finalresult;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            ArrayList<CheckBox> checkboxes = new ArrayList<CheckBox>();
            Veg = (CheckBox) findViewById(R.id.checkBox1);
            Nonveg = (CheckBox) findViewById(R.id.checkBox2);
            Bevarges = (CheckBox) findViewById(R.id.checkBox3);
            SoftDrink = (CheckBox) findViewById(R.id.checkBox4);
            HardDrink = (CheckBox) findViewById(R.id.checkBox5);
            FastFood = (CheckBox) findViewById(R.id.checkBox6);
            NorthIndian = (CheckBox) findViewById(R.id.checkBox7);
            SouthIndain = (CheckBox) findViewById(R.id.checkBox8);
            Dessert = (CheckBox) findViewById(R.id.checkBox9);
            SeaFood = (CheckBox) findViewById(R.id.checkBox10);
            Italian = (CheckBox) findViewById(R.id.checkBox11);
            Chiness = (CheckBox) findViewById(R.id.checkBox12);
            Maxican = (CheckBox) findViewById(R.id.checkBox13);
            Spanish = (CheckBox) findViewById(R.id.checkBox14);
            Thai = (CheckBox) findViewById(R.id.checkBox15);
            save = (Button) findViewById(R.id.button);

            final StringBuffer result = new StringBuffer();


            Veg.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    if (Veg.isChecked()) {

                        System.out.println("Veg checked");

                        if (result.length() >= 15) {
                            finalresult = result.substring(0, result.length() - 1);
                            Veg.setEnabled(false);
                            Veg.setChecked(false);
                        } else {
                            result.append("1, ");
                            System.out.println(result.length());
                        }

                    } else {
                        System.out.println("unchecked");
                        if (result.length() >= 15) {
                            Veg.setEnabled(false);
                            Veg.setChecked(true);
                        } else {
                            System.out.println("delete");
                            if (result.length() >=15){
                                finalresult = result.substring(0, result.length() - 3);
                            }
                        }
                    }

                }
            });

            Nonveg.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if (Nonveg.isChecked()) {

                        System.out.println("Nonveg checked");

                        if (result.length() >= 15) {
                            finalresult = result.substring(0, result.length() - 1);

                            Nonveg.setEnabled(false);
                            Nonveg.setChecked(false);
                        } else {
                            result.append("2, ");
                        }
                    } else {
                        System.out.println("Nonveg unchecked");

                        if (result.length() >= 15) {
                            Nonveg.setEnabled(false);
                            Nonveg.setChecked(true);
                        } else {
                            System.out.println("nondelete");
                        }
                    }
                }
            });

            Bevarges.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if (Bevarges.isChecked()) {
                        if (result.length() >= 15) {
                            finalresult = result.substring(0, result.length() - 1);

                            Bevarges.setEnabled(false);
                            Bevarges.setChecked(false);
                        } else {
                            result.append("3, ");
                        }
                    } else {
                        if (result.length() >= 15) {
                            Bevarges.setEnabled(false);
                            Bevarges.setChecked(true);
                        } else {

                        }

                    }
                }
            });

            SoftDrink.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if (SoftDrink.isChecked()) {
                        if (result.length() >= 15) {
                            finalresult = result.substring(0, result.length() - 1);

                            SoftDrink.setEnabled(false);
                            SoftDrink.setChecked(false);
                        } else {
                            result.append("4, ");
                            System.out.println(result.length());
                        }
                    } else {
                        if (result.length() >= 15) {
                            SoftDrink.setEnabled(false);
                            SoftDrink.setChecked(true);
                        } else {

                        }
                    }
                }
            });

            HardDrink.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if (HardDrink.isChecked()) {
                        if (result.length() >= 15) {
                            finalresult = result.substring(0, result.length() - 1);
                            HardDrink.setEnabled(false);
                            HardDrink.setChecked(false);
                        } else {
                            result.append("5, ");
                            System.out.println(result.length());
                        }
                    } else {
                        if (result.length() >= 15) {
                            HardDrink.setEnabled(false);
                            HardDrink.setChecked(true);
                        } else {

                        }
                    }
                }
            });

            FastFood.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if (FastFood.isChecked()) {
                        if (result.length() >= 15) {
                            finalresult = result.substring(0, result.length() - 1);
                            FastFood.setEnabled(false);
                            FastFood.setChecked(false);
                        } else {
                            result.append("6, ");
                        }
                    } else {
                        if (result.length() >= 15) {
                            FastFood.setEnabled(false);
                            FastFood.setChecked(true);
                        } else {

                        }
                    }
                }
            });

            NorthIndian.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if (NorthIndian.isChecked()) {
                        if (result.length() >= 15) {
                            finalresult = result.substring(0, result.length() - 1);
                            NorthIndian.setEnabled(false);
                            NorthIndian.setChecked(false);
                        } else {
                            result.append("7, ");
                        }
                    } else {
                        if (result.length() >= 15) {
                            NorthIndian.setEnabled(false);
                            NorthIndian.setChecked(true);
                        } else {

                        }
                    }
                }
            });

            SouthIndain.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if (SouthIndain.isChecked()) {
                        if (result.length() >= 15) {
                            finalresult = result.substring(0, result.length() - 1);
                            SouthIndain.setEnabled(false);
                            SouthIndain.setChecked(false);
                        } else {
                            result.append("8, ");
                        }
                    } else {
                        if (result.length() >= 15) {
                            SouthIndain.setEnabled(false);
                            SouthIndain.setChecked(true);
                        } else {
                        }
                    }
                }
            });

            Dessert.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if (Dessert.isChecked()) {
                        if (result.length() >= 15) {
                            finalresult = result.substring(0, result.length() - 1);
                            Dessert.setEnabled(false);
                            Dessert.setChecked(false);
                        } else {
                            result.append("9, ");
                        }
                    } else {
                        if (result.length() >= 15) {

                            Dessert.setEnabled(false);
                            Dessert.setChecked(true);
                        } else {

                        }
                    }
                }
            });

            SeaFood.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if (SeaFood.isChecked()) {
                        if (result.length() >= 15) {
                            finalresult = result.substring(0, result.length() - 1);
                            SeaFood.setEnabled(false);
                            SeaFood.setChecked(false);
                        } else {
                            result.append("10,");
                        }
                    } else {
                        if (result.length() >= 15) {

                            SeaFood.setEnabled(false);
                            SeaFood.setChecked(true);
                        } else {

                        }
                    }
                }
            });

            Italian.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if (Italian.isChecked()) {
                        if (result.length() >= 15) {
                            finalresult = result.substring(0, result.length() - 1);
                            Italian.setEnabled(false);
                            Italian.setChecked(false);
                        } else {
                            result.append("11,");
                        }
                    } else {
                        if (result.length() >= 15) {

                            Italian.setEnabled(false);
                            Italian.setChecked(true);
                        } else {

                        }
                    }
                }
            });

            Chiness.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if (Chiness.isChecked()) {
                        if (result.length() >= 15) {
                            finalresult = result.substring(0, result.length() - 1);
                            Chiness.setEnabled(false);
                            Chiness.setChecked(false);
                        } else {
                            result.append("12,");
                        }
                    } else {
                        if (result.length() >= 15) {

                            Chiness.setEnabled(false);
                            Chiness.setChecked(true);
                        } else {

                        }
                    }
                }
            });

            Maxican.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if (Maxican.isChecked()) {
                        if (result.length() >= 15) {
                            finalresult = result.substring(0, result.length() - 1);
                            Maxican.setEnabled(false);
                            Maxican.setChecked(false);
                        } else {
                            result.append("13,");
                        }
                    } else {
                        if (result.length() >= 15) {

                            Maxican.setEnabled(false);
                            Maxican.setChecked(true);
                        } else {

                        }
                    }
                }
            });

            Spanish.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if (Spanish.isChecked()) {
                        if (result.length() >= 15) {
                            finalresult = result.substring(0, result.length() - 1);
                            Spanish.setEnabled(false);
                            Spanish.setChecked(false);
                        } else {
                            result.append("14,");
                        }
                    } else {
                        if (result.length() >= 15) {

                            Spanish.setEnabled(false);
                            Spanish.setChecked(true);
                        } else {

                        }
                    }
                }
            });

            Thai.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if (Thai.isChecked()) {
                        if (result.length() >= 15) {
                            finalresult = result.substring(0, result.length() - 1);
                            Thai.setEnabled(false);
                            Thai.setChecked(false);
                        } else {
                            result.append("15,");
                        }
                    } else {
                        if (result.length() >= 15) {

                            Thai.setEnabled(false);
                            Thai.setChecked(true);
                        } else {

                        }
                    }
                }
            });


        Veg.setOnCheckedChangeListener(this);   {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub

            String checkboxes;
            CheckBox checkbox;
            if (checkboxes.contains((CharSequence) checkbox)) {
                ((CompoundButton) checkbox).setChecked(false);
                checkboxes.remove(checkbox);
            } else {
                if (checkboxes.size() < 5) {
                    checkbox.setChecked(true);
                    checkboxes.add(checkbox);
                } else {
                    checkbox.setChecked(false);
                }
            }
        }
        }); 
    }
    }

3 个答案:

答案 0 :(得分:0)

您可以将选中的复选框放入ArrayList<CheckBox>onCheckedChangeListener使用CheckBox确认已选中的arrayList.contains(checkedCheckbox)在列表中。如果true,请取消选中。如果false检查ArrayList#size()。如果小于5,请检查它。否则返回

ArrayList<CheckBox> checboxes = new ArrayList<CheckBox>();


// The containing class should implement OnCheckedChangeListener
checkbox1.setOnCheckedChangeListener(this);
checkbox2.setOnCheckedChangeListener(this);
checkbox3.....

@Override
public void onCheckedChange(CompoundButton checkbox, boolean isChecked) {

   if(checkboxes.contains(checkbox)) {
       checkbox.setChecked(false);
       checkboxes.remove(checkbox);
   }
   else {
       if(checkboxes.size() < 5) {
           checkbox.setChecked(true);
           checkboxes.add(checkbox);
       } else {
           checkbox.setChecked(false);
       }
   }
}

编辑:您的问题在这里,这不是您实现界面的方式

Veg.setOnCheckedChangeListener(this);   {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO Auto-generated method stub

        String checkboxes;
        CheckBox checkbox;
        if (checkboxes.contains((CharSequence) checkbox)) {
            ((CompoundButton) checkbox).setChecked(false);
            checkboxes.remove(checkbox);
        } else {
            if (checkboxes.size() < 5) {
                checkbox.setChecked(true);
                checkboxes.add(checkbox);
            } else {
                checkbox.setChecked(false);
            }
        }
    }
    }); 

正确的方法是:

 @Override
 public void onCreate(Bundle savedInstanceState) {
       // Here goes your onCreate's code and the checkbox reference etc...

       // DO NOT set a single listener for each checkbox, instead pass "this"
       // Also, you don't need to use `setOnClickListener`
       Veg.setOnCheckedChangeListener(this);
       Nonveg.setOnCheckedChangeListener(this);
 }

 @Override
 public void onCheckedChangeListener(CompoundButton checkbox, boolean isChecked) {
      // Here define your logic, possibly put a similar implementation from my original answer.

      // if you NEED to implement distinct logic for each checkbox, do the following:
      switch(checkbox.getId()) {
          case R.id.checkBox1:
                // some logic
                break;
          case R.id.checkBox2:

                //..... etc
      }

 }

提醒您,请确保您导入 CompoundButton.OnCheckedChangeListener而不是RadioGroup.OnCheckedChangeListener

答案 1 :(得分:0)

您必须创建一个简单字符串数组:

ArrayList<String> str=new ArrayList<String>();

复选框更改事件:

if(isChecked) {
    if(str.size()<5) {
        str.add(ch.getText().toString());
    } else {
        ch.setChecked(false);
    }
} else {
    str.remove(ch.getText().toString());
}

答案 2 :(得分:0)

我想你希望用户选择15个可用的最多5个选项。如果是这样,您可以尝试以下操作:

ArrayList<checkBox> checkBoxes; // add all your checBoxes to this Structure and set their tag as 0 to 14
ArrayList<Integer> selectedIndexes = new ArrayList<Integer>();

class checkListener implements View.onClickListener//Set this listener to all checkboxes
{
 @Override
 public void onClick(View v)
{
int currIndex = v.getTag();
if(checkBoxes.get(currIndex).isChecked)
{
//already checked, just un-check it and remove it from selectedIndexes
checkBoxes.get(currIndex).setChecked(false);
selectedIndexes.remove(currIndex);
}
else
{
//The current is unChecked and wishes to be checked
if(selectedIndexes.size()<5)
{
//if no of checked elements < 5, simply check it
checkBoxes.get(currIndex).setChecked(true);
selectedIndexes.add(currIndex);
}
else
{
//or else if already 5 checked, uncheck the last checked and mark the current as checked
int lastChecked = selectedIndexes.get(selectedIndexes.size()-1);
checkBoxes.get(lastChecked).setChecked(false);
selectedIndexes.remove(lastChecked);
checkBoxes.get(currIndex).setChecked(true);
selectedIndexes.add(currIndex);
}
}
}
}

最后,您可以遍历checkBoxes arrayList以获取所有选中的内容。 希望这有点帮助你!