我正在构建一个使用计时器的应用程序。我有一个带启动和停止按钮的简单计时器。它可以工作,但我希望秒数重置为60.目前它继续运行61,62,63等。我可以得到一些帮助吗?下面是我的启动方法,停止方法和具有我的计时器的runnable。
private long startTime = 0;
private long timeInMilliSeconds = 0;
private long updatedTime = 0;
private long timeSwapBuff = 0;
start.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timeSwapBuff += timeInMilliSeconds;
customHandler.removeCallbacks(updateTimerThread);
}
});
private Runnable updateTimerThread = new Runnable() {
@Override
public void run() {
timeInMilliSeconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliSeconds;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
int milliseconds = (int) (updatedTime % 1000);
timer.setText("" + mins + ":" + String.format("%02d", secs) +
":" + String.format("%02d", milliseconds));
customHandler.postDelayed(this, 0);
}
};
答案 0 :(得分:1)
您考虑打印time%60
这样的
int timeEllapsed = ... // the actual time spent for eg lets say 98
int timeToBePrinted = timeEllapsed%60 // the time you are going to show (38 seconds)