Reputation: 11
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
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