1

I have 4 buttons (a1, a2, b1, b2). They can combinate: a1-b1; a1-b2; a2-b1; a2-b2.It means that "a1" and "a2" cann't be chosen together. So after clicking "a1" I need to block "a2".And depends on what 2 buttons were pressed, it will be differenet actions.I tried something like this

if(a1.isPressed()|| b1.isPressed()){
a2.setClickable(false);
b2.setClickable(false);}

but it didn't work.

Update:

I tried to add boolean variable a1Boolean = false; boolean a2Boolean = false; And after each click `a1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            a1Boolean = true;`

And then to check in the method

private boolean geta1_b1() { if (a1Boolean || b1Boolean) { a2.setClickable(false); b2.setClickable(false); } return true; }

but when I call this method before all these clicks, of corse, it didn't work, because I didn't press any buttons yet. Any ideas how to find a right decision?

blahdiblah
  • 33,069
  • 21
  • 98
  • 152
Viktoriya
  • 19
  • 5

4 Answers4

1

Use else if statements. That is your best bet for something like this imo.

if(a1.isPressed()){
        a2.setClickable(false);
    }else if(b1.isPressed()){
        b2.setClickable(false)
    }

Make sure that you clear the state when appropriate.

hwrdprkns
  • 7,525
  • 12
  • 48
  • 69
1

Why don't you use RadioGroups?

Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • Because it will be image inside the button. so, i cann't use radiobutton – Viktoriya Jun 22 '11 at 13:58
  • You should consider using radio buttons for 2 reasons : 1) it would be easier to code, 2) it will be easier for users to recognize that the choices provided by your buttons are mutually exclusive. If you need image inside your buttons, you could consider designing custom radio buttons as proposed here : http://stackoverflow.com/questions/3576507/is-it-possible-to-change-the-radio-button-icon-in-an-android-radio-button-group – Snicolas Jun 22 '11 at 16:07
1

you may try RadioGroup and RadioButton and check which one is selected.

Girish Bhutiya
  • 3,111
  • 5
  • 31
  • 50
  • something like this? private boolean geta1_b1() { if (a1.isChecked()==true || b1.isChecked()==true) { a2.setClickable(false); b2.setClickable(false); return true; } return false; } – Viktoriya Jun 23 '11 at 08:47
1

you may invisible button like:

if(a1.isPressed()){ a2.setVisibility(false); }else if(b1.isPressed()){ b2.setVisibility(false) }

Girish Bhutiya
  • 3,111
  • 5
  • 31
  • 50
  • firstly, setVisibility couldn't be false, because "public void setVisibility (int visibility) Since: API Level 1 Set the enabled state of this view. " And even I write a2.setVisibility(View.GONE); - it doesn't work – Viktoriya Jun 23 '11 at 11:32