gpt4 book ai didi

java - 更改文本按钮,反之亦然 Android

转载 作者:行者123 更新时间:2023-11-30 03:01:38 25 4
gpt4 key购买 nike

我的 xml 中有这个按钮

我想使用它两次,一次用于更改另一个按钮的值,第二次用于像以前一样获取值。

这是我的:

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 的值,因此,当用户想要更改文本或将其恢复原样时,我如何使用该按钮来设置文本?

最佳答案

将该变量作为全局变量放在函数之外。

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;
}
}
}

关于java - 更改文本按钮,反之亦然 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22427288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com