user3423781
user3423781

Reputation: 11

Change text button and vice versa Android

I have this button in my xml

And I want to use it twice, one for change another buttons values and the secod for get the values as before.

This is what I have:

public void cambia(View view) {
    boolean a = true;
    Button boton1 = (Button)this.findViewById(R.id.btnDividir);
    Button boton2 = (Button)this.findViewById(R.id.btnMultiplicar);
    Button boton3 = (Button)this.findViewById(R.id.btnRestar);
    Button boton4 = (Button)this.findViewById(R.id.btnSumar);
    if (a == true) {
        boton1.setText("/");
        boton2.setText("*");
        boton3.setText("-");
        boton4.setText("+");
        a = false;
    } else if(a == false) {
        boton1.setText("divide");
        boton2.setText("multiplica");
        boton3.setText("resta");
        boton4.setText("suma");
        a = true;
    }
}

But my variable a is going to always have the value of true, so, How can I use that button to set the text whenever the user wants to change it or get it back as it was?

Upvotes: 1

Views: 114

Answers (1)

Nizam
Nizam

Reputation: 5731

Put that variable outside the function, as a global one.

class YourClass extends Activity{
    boolean a = true;
    ....
    ...
    public void cambia(View view) {
        Button boton1 = (Button)this.findViewById(R.id.btnDividir);
        Button boton2 = (Button)this.findViewById(R.id.btnMultiplicar);
        Button boton3 = (Button)this.findViewById(R.id.btnRestar);
        Button boton4 = (Button)this.findViewById(R.id.btnSumar);
        if (a == true) {
            boton1.setText("/");
            boton2.setText("*");
            boton3.setText("-");
            boton4.setText("+");
            a = false;
        } else {
            boton1.setText("divide");
            boton2.setText("multiplica");
            boton3.setText("resta");
            boton4.setText("suma");
            a = true;
        }
    }
}

Upvotes: 1

Related Questions