Disable a button

1

Good afternoon, I was trying and I do not understand how to do it. I want the user to choose an option, by pressing a button, depending on the button you have chosen, a specific action takes place. The problem is that if you press the A button, I want the B button to be disabled and vice versa I can not do it. Can someone give me an idea of how to do it? Thanks

    
asked by Agustin Val 10.09.2016 в 16:11
source

3 answers

2

To disable a button, it is done using the android:enabled property where the boolean value determines if the view is enabled or disabled:

The default of the view is android:enabled="true" whereby the view is enabled:

<Button
    android:text="Boton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/button"
    android:enabled="true"/>

can be done programmatically by:

Button boton = (Button) findViewById(R.id.button);
boton.setEnabled(true);  //Asigna valor true.

Disable button:

To disable a button is done using the property android:enabled="false"

<Button
    android:text="Boton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/button"
    android:enabled="true"/>

can be done programmatically by:

Button boton = (Button) findViewById(R.id.button);
boton.setEnabled(false); //Asigna valor false.

In the case that you comment, add two buttons in your layout:

and using a onClickListener change enable / disable the button:

    final Button botonA = (Button) findViewById(R.id.buttonA);
    final Button botonB = (Button) findViewById(R.id.buttonB);
    botonA.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            botonB.setEnabled(false);
        }
    });

    botonB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            botonA.setEnabled(false);
        }
    });

For what you need is to choose a button and disable others I think it would be better to use a RadioGroup containing several RadioButton where selecting only one element would be enabled.

    
answered by 19.10.2016 / 18:43
source
1

interesting your question, you can try with this code:

Button btn = (Button) findViewById(R.id.button1);
btn.setEnabled(false);

When you want to enable the same button just type

Button btn = (Button) findViewById(R.id.button1);
btn.setEnabled(true);

Knowing that, I think it's just playing with the If and the else between the two buttons.

I hope I have helped you!

  

@nickoelton

    
answered by 12.09.2016 в 19:23
1

Something like that?

mButtonA.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        mButtonB.setEnabled(false);
    }
});


mButtonB.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        mButtonA.setEnabled(false);
    }
});

Visually I would recommend using a CheckButton link link

    
answered by 12.09.2016 в 19:33