我的xml中有这个按钮
我想要使用它两次,一次用于更改另一个按钮值,而secod用于获取之前的值。
这就是我所拥有的:
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;
}
}
但我的变量a将始终具有true的值,因此,如果用户想要更改它或将其恢复原状,我该如何使用该按钮来设置文本?
答案 0 :(得分:1)
将该变量置于函数之外,作为全局变量。
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;
}
}
}