gpt4 book ai didi

Android Textview 不更新

转载 作者:行者123 更新时间:2023-11-29 14:32:58 25 4
gpt4 key购买 nike

大家。我正在尝试为 Android 制作一款基本的大亨游戏我正在尝试使用计时器每 5 秒增加一次 TextView 的值,但是 textview 没有更新。到目前为止,这是我的代码:

public class Town extends Activity implements OnClickListener {
Timer timer;
TimerTask task;
TextView goldTV;
TextView woodTV;
TextView foodTV;
TextView stoneTV;
TextView cashTV;
int gold = 20;
int wood = 20;
int food = 20;
int stone = 20;
int cash = 200;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_town);
goldTV = (TextView) findViewById(R.id.textView1);
woodTV = (TextView) findViewById(R.id.TextView01);
foodTV = (TextView) findViewById(R.id.TextView02);
stoneTV = (TextView) findViewById(R.id.TextView03);
cashTV = (TextView) findViewById(R.id.TextView04);
timer = new Timer();
task = new TimerTask() {

@Override
public void run() {
gold++;
goldTV.setText(gold);
try {
this.wait(2000);
}
catch (InterruptedException e){
}
}

};
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

}

最佳答案

在您的run() 方法中

@Override
public void run() {
gold++;
goldTV.setText(gold);

try {
this.wait(2000);
}
catch (InterruptedException e){

}
}

您正在调用 setText(int resId) 而不是 setText(CharSequence c);

要显示实际的整数 gold,将其从 int 转换为 String

String goldStr = String.valueOf(gold);

goldTV.setText(goldStr);

关于Android Textview 不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13595056/

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