【发布时间】:2012-11-30 18:57:53
【问题描述】:
每个人。 我正在尝试为 Android 制作一个基本的大亨游戏 我正在尝试使用计时器每 5 秒增加一次文本视图的值, 但是文本视图没有更新。 到目前为止,这是我的代码:
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
}
}
【问题讨论】:
标签: android multithreading timer textview