【发布时间】:2012-05-12 11:51:04
【问题描述】:
我正在为 android 制作秒表应用程序。我有一个启动和停止按钮。我的问题是时间增长得非常快。我哪里错了。下面是代码:
final Runnable updater = new Runnable() {
public void run() {
if (startIsPressed) {
time = SystemClock.elapsedRealtime() - initStart + startPointTime;
startPointTime = time;
} else {
time = startPointTime;
}
hh = time / 3600000;
hours.setText("" + formatter.format(hh));
time = time - hh * 3600000;
mm = time / 60000;
minutes.setText("" + formatter.format(mm));
time = time - mm * 60000;
ss = time / 1000;
seconds.setText("" + formatter.format(ss));
time = time - ss * 1000;
millis.setText("" + formatter.format(time / 10));
handler.postDelayed(this, 30);
}
};
startBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (!startIsPressed) {
startIsPressed = true;
startBtn.setText(R.string.stop);
initStart = SystemClock.elapsedRealtime();
handler.post(updater);
} else {
startIsPressed = false;
startBtn.setText(R.string.start);
handler.post(updater);
}
}
});
}
【问题讨论】:
-
看到这篇文章可能会有所帮助stackoverflow.com/questions/3733867/stop-watch-logic
-
感谢重播,但我的问题仍然是如何开始计算停止的时间。链接中显示的示例,仅停止计时器并重新开始。