【问题标题】:Change text button and vice versa Android更改文本按钮,反之亦然 Android
【发布时间】:2014-03-15 17:31:30
【问题描述】:

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

【问题讨论】:

  • 你可以使用切换按钮

标签: java android button


【解决方案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;
        }
    }
}

【讨论】:

  • 效果很好!谢谢大家!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多