当计时器在倒计时中途时,CountDownTimer的OnFinish()将触发

时间:2013-07-18 16:19:36

标签: android countdowntimer

我在Android项目中使用了CountDownTimer,结果发生了,当我的OnFinish()在CountDown处于中途阶段时触发。

以下是代码:

public void ShowNotice(){
        cdt = new CountDownTimer(10000, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                // TODO Auto-generated method stub
                Toast toast = Toast.makeText(con,"Game Starts In :"+String.valueOf(millisUntilFinished/1000),
                        Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
                toast.show();
            }

            @Override
            public void onFinish() {
                // TODO Auto-generated method stub
                RemoveButtonText();
            }
        };

        cdt.start();
    }

RemoveButtonText(); //即使没有完成倒计时也会执行此方法,即Toast显示'5'。

注意:cdt在CountDownTimer类型的类中声明为私有成员变量。

需要帮助:)

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题。

public void ShowNotice(){
        cdt = new CountDownTimer(20000, 2000) {

            @Override
            public void onTick(long millisUntilFinished) {
                // TODO Auto-generated method stub
                Toast toast = Toast.makeText(con,"Game Starts In :"+String.valueOf(millisUntilFinished/2000),
                        Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.BOTTOM, 0, 0);
                toast.show();
            }

            @Override
            public void onFinish() {
                // TODO Auto-generated method stub
                Toast toast = Toast.makeText(con,"Game Starts Now!",
                        Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.BOTTOM, 0, 0);
                toast.show();
                RemoveButtonText();
            }
        };

        cdt.start();
    }

抓住了这个

cdt = new CountDownTimer(20000, 2000)

右。

谢谢:)