【问题标题】:androidL how to reset a timerandroid如何重置计时器
【发布时间】:2013-10-06 17:15:10
【问题描述】:

我正在学习使用计时器,并按照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,以便在再次按下按钮时重新计数?谢谢!!

【问题讨论】:

  • 你不能使用一个在 ACTION_UP 时才为假的布尔值吗?如果 ACTION_DOWN 时为 false,则将其变为 true 并重置,否则 count++

标签: android time timer android-5.0-lollipop


【解决方案1】:

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

总之,感谢莫妮卡介绍天文台表,看起来不错!并感谢 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】:

    尝试在android中使用chronometer...你可以使用它的功能来停止、启动、重启,设置为0

    【讨论】:

      【解决方案3】:

      仅当您的计时器不需要毫秒时才使用 Chronometer。

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

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

      试试:

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

      【讨论】:

        猜你喜欢
        • 2021-03-11
        • 2011-12-28
        • 1970-01-01
        • 1970-01-01
        • 2011-06-03
        • 1970-01-01
        • 2010-11-05
        相关资源
        最近更新 更多