Checkbox selection

Asked

Viewed 44 times

-1

Good night,

In my app, I need to select a total of 2 Checkbox boxes. After selecting 2 boxes, all others should be set with setEnabled(false).

So far so good, but how will I know which box was selected, WITHOUT KNOWING which box the user choose, and how will I simply give a setEnabled(false) in all others?

The logic would be like:

if(duasCheckBoxQualquer.isClick(){ todasAsOutrasCheckBox.setEnabled(false)

If anyone can help me, I’d appreciate it.

1 answer

1

Gustavo, I don’t know if it would be the best, but one option would be to start an int variable and increment it or decrement it as it was marked or unchecked Checkboxes with a setOnCheckedChangeListener, and when incrementing the variable, run a method to uncheck the others...

checkBoxClicada.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
      if(isChecked) //ao marcar a checkbox
          {
              valorInteiro = valorInteiro + 1;
              if(valorInteiro == 2)
              {
                métodoParaDesabilitarOutrasCheckBoxesNãoMarcadas();
              }  
           }
       else //ao desmarcar checkbox
          {
              valorInteiro = valorInteiro - 1; 
              métodoParaHabilitarTodasAsCHeckBoxes();   
           }


    }

I do not know if I was clear... anything, comment on the answer... I hope to have helped :)

  • Thanks for the answer! I’m doing it this way anyway, I’ll test and tell you.

  • Yeah, that’s right, he selects two boxes and disables them all. I figured that would happen. The problem now is how to make all, except the selected, be disabled.

  • Condition the boxes before disabled...

  • If(checkBox1.isChecked()) { //not disable } get it? It will definitely work

  • If the answer has been solved, mark it as correct :)

  • I hope I’ve helped

  • You gave me an idea... I made the selection equal to 2, and cleared all Checkboxes. Correct. After that, the chosen Checkbox receives a setEnabled(true). This did not work :c

  • Yes, we are going:)

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.