安卓:完成()CountDownTimer称为即使取消()被调用 [英] Android: finish() of CountDownTimer is called even if cancel() is called

查看:1009
本文介绍了安卓:完成()CountDownTimer称为即使取消()被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用倒数计时器这样

新CountDownTimer(15000,15){

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(); 

在一个按钮,单击我呼吁取消(),但它停止计数了一会儿,然后调用onFinish()。我不需要调用cancel()之后调用onFinish()有什么解决这一点。任何帮助将是非常美联社preciated。

On a button click I have called cancel() but it stops counting for a while and then calls onFinish(). I need not to call onFinish() after calling cancel() is there any solution for this. Any help will be highly appreciated.

推荐答案

在你的的onClick 设置一个布尔(按钮pressed为例)为true。

Inside your onClick set a Boolean (buttonPressed for example) to true.

在你的 onFinish 检查这个布尔:

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

这篇关于安卓:完成()CountDownTimer称为即使取消()被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆