对于我的Android应用程序,有一个计时器来测量经过了多少时间。每100毫秒,我会更新我的TextView,显示一些文本,比如“得分:10 时间:100.10秒”。但是,我发现TextView只会在最开始的几次更新。应用程序仍然非常响应,但标签不会更新。我尝试调用.invalidate(),但它仍然无法工作。我不知道是否有一种方法来解决这个问题,或者使用更好的小部件。
这里是我的代码示例:
float seconds;
java.util.Timer gametimer;
void updatecount() { TextView t = (TextView)findViewById(R.id.topscore);
t.setText("Score: 10 - Time: "+seconds+" seconds");
t.postInvalidate();
}
public void onCreate(Bundle sis) {
... Load the UI, etc...
gametimer.schedule(new TimerTask() { public void run() {
seconds+=0.1; updatecount();
} }, 100, 100);
}