android如何重置计时器

时间:2013-10-06 17:15:10

标签: android time timer android-5.0-lollipop

我正在学习使用计时器,并按照http://examples.javacodegeeks.com/android/core/os/handler/android-timer-example/中的示例进行操作。

我想以一种方式实现,当用户按下按钮时定时器将启动,当用户的手关闭时停止,所以我编码如下:

代码:

button_right.setOnTouchListener( new View.OnTouchListener() 
{
    @Override
    public boolean onTouch(View arg0, MotionEvent event) 
    {
        if(event.getAction()==MotionEvent.ACTION_DOWN )
        {
            startTime = SystemClock.uptimeMillis();
            customHandler.postDelayed(updateTimerThread, 0);



        if((event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL))
        {
            timeSwapBuff += timeInMilliseconds;
            customHandler.removeCallbacks(updateTimerThread);
        }
        return false;
    }
});  



// setting timer
private Runnable updateTimerThread = new Runnable() 
{
    public void run() 
    {
        timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
        updatedTime = timeSwapBuff + timeInMilliseconds;

        int secs = (int) (updatedTime / 1000);
        int mins = secs / 60;
        secs = secs % 60;
        int milliseconds = (int) (updatedTime % 1000);
        tv_timing.setText("" + mins + ":" + String.format("%02d", secs) + ":" + String.format("%03d", milliseconds));
        customHandler.postDelayed(this, 0);
    }
};      

问题:

一切正常,定时器将在用户按下按钮时启动,在按住时保持运行并在手动关闭时停止。然而,我发现当用户再次按下按钮时,计时器从上次停止的位置开始,而不是在计算时间之前重置为0.

如果使用此代码,如何修改定时器重置为0,以便在再次按下按钮时重新计数?谢谢!

3 个答案:

答案 0 :(得分:0)

尝试在android中使用chronometer ...您可以使用其功能来停止,启动,重启,将其设置为0

答案 1 :(得分:0)

在代码中挖掘更多细节并通过网络进一步研究,我找到了答案并修改了如下代码并且它有效。

总而言之,感谢Monica推出天文台,它看起来不错!并感谢Zyoo让我在ACTION_DOWN

下制作一个removeCallbacks
            if(event.getAction()==MotionEvent.ACTION_DOWN )
            {
                if(startTime == 0L)
                {
                    startTime = SystemClock.uptimeMillis();
                    customHandler.removeCallbacks(updateTimerThread);
                    customHandler.postDelayed(updateTimerThread, 0);
                }

            if((event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL))
            {
                // timeSwapBuff += timeInMilliseconds; //remove this! 
                customHandler.removeCallbacks(updateTimerThread);
                startTime = 0L;
            }
            return false;


private Runnable updateTimerThread = new Runnable() 
{
    public void run() 
    {
        timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
        //updatedTime = timeSwapBuff + timeInMilliseconds;  //remove this!! else starting from where it stops last time!

        int secs = (int) (timeInMilliseconds / 1000);
        int mins = secs / 60;
        secs = secs % 60;
        int milliseconds = (int) (timeInMilliseconds % 1000);
        tv_timing.setText("" + mins + ":" + String.format("%02d", secs) + ":" + String.format("%03d", milliseconds));
        customHandler.postDelayed(this, 0);
    }
};

答案 2 :(得分:0)

如果你的计时器不需要毫秒,只能使用天文台。

重置计时器的另一个选项是添加以下1行:timeSwapBuff = 0L;。它比删除2个单独的更简单。

在“开始”按钮的onClick事件中进行此更改。这会将时间缓冲区重置为0,然后将其添加回startTime(也为0)并强制计时器重新开始。

尝试:

public void onClick(View view) {
    timeSwapBuff = 0L;
    startTime = SystemClock.uptimeMillis();
    customHandler.postDelayed(updateTimerThread, 0);
}