微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我这样使用了Countdown Timer

新的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().我不需要在调用cancel()之后调用onFinish()是否有任何解决方案.任何帮助将受到高度赞赏.

解决方法:

在onClick中将布尔值(例如buttonpressed)设置为true.

在你的onFinish中检查这个布尔值:

if (buttonpressed == true)
{
    //do nothing
}
else
{
    //run code
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐