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
}

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...