需要安卓计时器秒重置为60

时间:2016-11-29 20:38:08

标签: java android timer

我正在构建一个使用计时器的应用程序。我有一个带启动和停止按钮的简单计时器。它可以工作,但我希望秒数重置为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);
        }
    };

1 个答案:

答案 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)