简体   繁体   中英

Limit number of checked checkboxes in Android

I have a problem with my application. I just want to set a limit of 2 checked checkboxes but I don't know how. I have 4 checkboxes and a button. When the button is pressed, if there are only 2 checked checkboxes do something, if are 3 or more do something else. Here is my code:

     button.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {

             if(chk1.isChecked()){
                  counter++;}
             else{
                 counter--;
             }
              if(chk2.isChecked())

              { counter++;}
              else{
                     counter--;
                 }
              if(chk3.isChecked())
              { counter++; }
              else{
                     counter--;
                 }
              if(chk4.isChecked())
              {  counter++; }
              else{
                     counter--;
                 }

             if ( (chk1.isChecked() || chk2.isChecked() || chk3.isChecked() || chk4.isChecked()) && counter >2 ) {

                 Toast.makeText(StartingPoint.this, "boo",  Toast.LENGTH_LONG).show();
                } 
             else {
                 Toast.makeText(StartingPoint.this, "no boo", Toast.LENGTH_LONG).show();
                }

            }


    });

If I understand you correctly, you nearly got it already.

  • Declare your counter
  • Forget about the decrements, only use increments
  • Use a 1st if/then statement to check how many checkboxes have been checked (the value of counter )
  • Use a nested if/then statement to apply logic based on the specific checkboxes

Edit - here's a simplified example.

        /*
         * This will check the number of CheckBoxes checked when your
         * button is pressed, and perform some logic consequently.
         * 
         * ALTERNATIVE (not shown here): 
         * If you wish to disable the button based on how many CheckBoxes
         * are checked, you should add an OnClickListener for each CheckBox
         * (I'm over-simplifying here on purpose). 
         * Each CheckBox click would increase the counter if checked, 
         * decrease it if not.
         * Each listener would decide whether or not to enable the button
         * after computing the counter's value.   
         */

        Button myButton = null; // TODO initialize properly
        // TODO initialize all CheckBoxes properly
        final CheckBox cb0 = null;
        final CheckBox cb1 = null;
        final CheckBox cb2 = null;
        // ... and so forth ...

        // Initializing the anonymous listener class attached to the Button
        myButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // initializing the counter each click
                int counter = 0;
                // incrementing the counter for each CheckBox checked
                if (cb0.isChecked()) counter++;
                if (cb1.isChecked()) counter++;
                if (cb2.isChecked()) counter++;
                // ... and so forth ...

                // Verifying number of CheckBoxes checked...
                // ...up to 2 CheckBoxes checked
                if (counter < 3) {
                    // TODO logic depending on which CheckBox(es)
                }
                // ...more than 2 CheckBoxes checked
                else {
                    // TODO logic depending on which CheckBox(es) or anything else
                }
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM