我正在制作一个统计计数器,计数到一定数量(比如60),用户应该看到每个数字在屏幕上递增。
TextView display1;
public int stat1 = 60;
public void doSomething(View v){
display1 = (TextView)findViewById(R.id.stat);
for(int i=0;i<=stat1;i++){
String temp = (String.valueOf(i));
for(long j=0;j<1000000;j++);
Log.d("PATO","Value - "+i);
display1.setText(temp);
display1.invalidate();
}
}
但是,此代码仅在屏幕上显示最终数字,即60,即使我可以在调试屏幕上看到增量。
答案 0 :(得分:1)
请找到以下解决方案
protected static void startTimer() {
isTimerRunning = true;
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
elapsedTime += 1; //increase every sec
mHandler.obtainMessage(1).sendToTarget();
}
}, 0, 1000);
};
public Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
StopWatch.time.setText(formatIntoHHMMSS(elapsedTime)); //this is the textview
}
}
以上代码必须有效......
注意:必须在主线程中创建mHandler。
答案 1 :(得分:-1)
As your counter is fast . so display1.setText() is overlapping the value, so you cannot see the previous value, so you can do this :
for(int i=0;i<=stat1;i++){
String temp = (String.valueOf(i));
Log.d("Value",temp);
Thread.sleep(1000);
for(long j=0;j<1000000;j++);
Log.d("PATO","Value - "+i);
display1.setText(temp);
display1.invalidate();
}
}