多倒数计时器运行此起彼伏 [英] Multiple CountDown Timer running one after another

查看:206
本文介绍了多倒数计时器运行此起彼伏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个问题在这里。
我需要在我的应用程序6 倒计时实施 计时器运行此起彼伏。当第一饰面,下一个开始,依此类推。 初始化时间的每一个运行依赖于用户输入。问题是,我需要把不同的code在 onTick() onFinish()每方法单倒计时运行的,那么,我不知道如何启动一个结束之后的下一个计数器。我在想关于 onFinish调用next 计数()当前的方法,但我无法弄清楚如何6 专柜做到这一点

这是我倒数计时器类:

 公共类反扩展CountDownTimer
{    公开计数器(长millisInFuture,长countDownInterval)
    {
        超(mil​​lisInFuture,countDownInterval);
    }    公共无效onTick(长millisUntilFinished)
    {
            //这个code是每一个柜台相同
            timer_view.setText(formatTime(millisUntilFinished));            //这个$ C $,c取决于具体背道而驰
            output_view1.setText(我在这里的输出);
            output_view2.setText(我在这里的输出);
            output_view3.setText(我在这里的输出);    }    公共无效onFinish()
    {
        playSound(sound_id_1);
        runMyMethod(user_input_1);        timerHasStarted = FALSE;
    }}

我开始我的柜台在同一个活动:

 如果(!timerHasStarted)
{
计数器=新的计数器(user_input1,1000);
    counter.start();
}


解决方案

您可能需要打破启动定时器functionallity并调用它 onFinish()

 公共无效startTimer所(INT counterId){
            专柜= NULL;
            开关(counterId){
                    情况下0:
                            计数器=新CounterOne(counterId,user_input1,1000);
                            打破;
                    / *计数器1-5到这里* /
                    默认:
                            打破;
            }
            如果(计数器!= NULL){
                    counter.start();
            }
    }

然后开始在onFinsh你的下一个定时器()

 公共抽象类反扩展CountDownTimer
    {
            私人诠释counterId;            公开计数器(INT counterId / *计数器号码从0开始* /长millisInFuture,长countDownInterval)
            {
                    超(mil​​lisInFuture,countDownInterval);
                    this.counterId = counterId;
            }            公共抽象无效onTick(长millisUntilFinished);            公共无效onFinish()
            {
                    playSound(sound_id_1);
                    runMyMethod(user_input_1);
                    startTimer所(this.counterId ++);
            }    }    公共类CounterOne扩展计数器{
            公共无效onTick(长millisUntilFinished)
            {
                    //计数器1的逻辑
            }
    }    / *子类等。例如。 CounterTwo等。* /

this is my first question here. I need to implement in my app six countdown timers running one after another. When first finishes, next one starts and so on. Init time of every single run depends on user input. The problem is that I need to put different code in onTick() and onFinish() methods for every single countdown timer running and, well, I'm not sure how to start a next counter after one is finished. I was thinking about calling next counter in onFinish() method of current one but I can't figure out how to do this with 6 counters.

This is my Countdown timer class:

public class Counter extends CountDownTimer
{

    public Counter(long millisInFuture, long countDownInterval)
    {
        super(millisInFuture, countDownInterval);
    }

    public void onTick(long millisUntilFinished)
    {
            //this code is the same for every counter
            timer_view.setText(formatTime(millisUntilFinished));

            //this code depends on specific counter running
            output_view1.setText("My output here");
            output_view2.setText("My output here");
            output_view3.setText("My output here");

    }

    public void onFinish()
    {
        playSound(sound_id_1);
        runMyMethod(user_input_1);

        timerHasStarted = false;


    }

}

I'm starting my counter in the same activity:

if(!timerHasStarted)
{
counter = new Counter(user_input1, 1000);
    counter.start();
}

解决方案

You probably need to break out the start timer functionallity and call it in onFinish().

    public void startTimer(int counterId){
            Counter counter = null;
            switch(counterId){
                    case 0:
                            counter = new CounterOne(counterId,user_input1,1000);
                            break;  
                    /* Counter 1-5  goes here*/
                    default:
                            break;
            }
            if(counter !=null ){
                    counter.start();
            }
    }

then start your next timer in onFinsh()

    public abstract class Counter extends CountDownTimer
    {
            private int counterId;

            public Counter(int counterId /*counter id start with 0*/,long millisInFuture, long countDownInterval)
            {
                    super(millisInFuture, countDownInterval);
                    this.counterId = counterId;
            }

            public abstract void onTick(long millisUntilFinished);

            public void onFinish()
            {
                    playSound(sound_id_1);
                    runMyMethod(user_input_1);
                    startTimer(this.counterId++);
            }

    }

    public class CounterOne extends Counter{
            public void onTick(long millisUntilFinished)
            {
                    //counter 1 logic
            }
    }

    /* Subclass others. eg. CounterTwo etc. */

这篇关于多倒数计时器运行此起彼伏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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