如何在Countdowntimer中更改tick

时间:2014-01-25 09:22:44

标签: java android countdowntimer

我有以下CountDownTimer帮助我设置每490毫秒30秒长的按钮。

是否可以说前10秒“滴答”应该是1000毫秒,接下来的10秒是700毫秒,最后10秒是490毫秒?

new CountDownTimer(30000, 490) {

@Override
public void onTick(long millisUntilFinished) {
    for(int i = 0; i< arr.size(); i++){
        Button aga = arr.get(i);
        if(aga.getVisibility() == View.VISIBLE){
            aga.setVisibility(View.GONE);
        }
    }
    int zufall = (int) (Math.random()*23);
    setNextButton(arr.get(zufall));
}

3 个答案:

答案 0 :(得分:0)

Timer mTimer; // Global 

public void countDownTimerCaller()
{
     static int count = 0;

     int time;

    switch(count)
    {
    case 0:
        time = 1000;
        break;
    case 1:
        time = 700;
        break;
    case 2:
        time = 490;
        break;

        default:
                mTimer.cancel(); // stop timer
            return;
    }

       new CountDownTimer(10000, time) {

        @Override
        public void onTick(long millisUntilFinished) {
            for(int i = 0; i< arr.size(); i++){
                Button aga = arr.get(i);
                if(aga.getVisibility() == View.VISIBLE){
                    aga.setVisibility(View.GONE);
                }
            }
            int zufall = (int) (Math.random()*23);
            setNextButton(arr.get(zufall));

        }

    }

    count++;
}

mTimer = new Timer().schedule(countDownTimerCaller, 10000); // call from where ever you created CountDownTimer instance

答案 1 :(得分:0)

除了按照这些间隔启动新的计时器来处理新的要求之外,最好的办法就是找到一个公约数,然后将其设置为你的间隔,然后使用一个带模数的开关按照你想要的时间行事。

答案 2 :(得分:0)

希望这会有所帮助。

public void countDown(long time) {
        if (time == 490) {
            return;
        }

        new CountDownTimer(10000, time) {

            public void onTick(long millisUntilFinished) {
                // Do whatever you want
             }

             public void onFinish() {
                 countDown(nextTime); // nextTime can be 700, 100, ... It's up to you. (Your rule). :">
             }

        }.start();

    }