修改textview的变量

时间:2015-01-12 10:19:50

标签: android

int triesno=5;
final TextView tries= (TextView) findViewById(R.id.tries);
tries.setText("you have "+triesno.toString()+" tries left");

我想制作一款游戏,当用户发送错误输入时,尝试次数减少。上面的代码打印了初始编号,但我想只修改变量而不是更新如下:

triesno--;
tries.setText("you have "+triesno.toString()+" tries left");

我该怎么做?

2 个答案:

答案 0 :(得分:0)

你确定你的问题吗?

final TextView tvTries = (TextView) findViewById(R.id.tries);
int tries = Integer.parseInt(tvTries.getText()); // Get current number of time that you can try to.
tvTries .setText("you have "+ --tries + " tries left"); // -1 and set to textview

答案 1 :(得分:0)

    int tries = 5;
    final TextView triesTv = (TextView) findViewById(R.id.tries);
    tries = tries - 1;
    String triesStr = String.format("you have: %d tries left", tries);
    triesTv.setText(triesStr);