Android:即使调用了cancel(),CountDownTimer的finish()也会被调用

2024-04-21

我这样使用倒数计时器

new CountDownTimer(15000, 15) {

             public void onTick(long millisUntilFinished) {

                 long seconds=millisUntilFinished/1000;
                 long min=millisUntilFinished%100;

                 timeleft=(int) (seconds*1000+min);
                 if(millisUntilFinished>=10000)
                 {
                     changeText.setTextColor(Color.GREEN);
                 }
                 else if(millisUntilFinished>=5000)
                 {
                     changeText.setTextColor(Color.MAGENTA); 
                 }
                 else
                 {
                     changeText.setTextColor(Color.RED);

                 }
                 changeText.setText(String.format("%02d", seconds )+ "."+String.format("%02d", min )+" sec");

             }

             public void onFinish() {

                 timeleft=0;
                 missed++;
                  nametext.setTextColor(Color.RED);
                 nametext.setText("Time Up!");
                      bottombutton.setVisibility(View.INVISIBLE);   
                    globalflag=13;
                changeText.setTextColor(Color.RED);
                changeText.setText("0.00 Sec");
                    Handler myHandler = new Handler();
                    myHandler.postDelayed(mMyRunnablecif, 3000);



             }
          }.start(); 

单击按钮我已拨打电话cancel()但它会停止计数一段时间然后调用onFinish()。我不需要打电话onFinish()打电话后cancel()。有没有什么解决办法呢。任何帮助将不胜感激。


在你的里面onClick将布尔值(例如,buttonPressed)设置为 true。

In your onFinish检查这个布尔值:

if (buttonPressed == true)
{
    //do nothing
}
else
{
    //run code
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android:即使调用了cancel(),CountDownTimer的finish()也会被调用 的相关文章

随机推荐