【问题标题】:Start another timer in Countdown timer在倒数计时器中启动另一个计时器
【发布时间】:2016-03-15 21:15:04
【问题描述】:

我在回合结束后设置暂停时遇到问题。当用户单击按钮时,它开始倒计时,当完成的文本返回到我想要的值时。但我希望在倒计时(计时器)完成时启动另一个 timer2。现在它同时开始计数。每个计时器在屏幕上都有自己的 textView。 这是代码:

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            long roundtime= Long.parseLong(String.valueOf(msg1))*1000; //User set Time for Round
            long pause= Long.parseLong(String.valueOf(msg2))*1000; //User set Time for Pause
            Counting timer = new Counting(roundtime, 1000); // class CountdownTimer
            Counting2 timer2 = new Counting2(pause, 1000); // class CountdownTimer

            timer.start(); // I want to start with this timer first
            timer2.start(); // And when timer is finished start with this in own textView
        }

enter image description here

【问题讨论】:

    标签: java android timer


    【解决方案1】:

    使用CountDownTimer。这就是你所需要的。只需启动一个计时器,然后在该计时器的 onFinish() 方法上,启动新计时器。比如:

    CountDownTimer timer1 = new CountDownTimer(30000, 1000) {
    
     public void onTick(long millisUntilFinished) {
    //do whatever you need here, this gets called every 1000 milliseconds (the 2nd parameter
    of the constructor, the first is total time in ms
     }
    
     public void onFinish() {
        CountDownTimer timer2 = new CountDownTimer(5000, 1000) {
            //same logic
            };
            timer2.start();
        }
    };
    

    timer1.start();

    【讨论】:

    • 嘿Vucko!我现在是你的意思,但我制作了两个不同的倒数计时器,因为我在屏幕上为每个计时器单独设置了两个文本视图。你知道我的意思吗?
    • 不是真的,想一想这门课,真的很好,我要睡觉了,明天如果你不能管理mate,会写信给你:)
    【解决方案2】:

    如果我正确理解了您的问题,您想要这样的东西。一旦您的活动开始,必须在“xx”秒后发生一些事情。然后必须有'yy'秒的暂停,然后同样的事情必须'xx'秒。对吧?

    你可以使用Handler.postDelayed()

    static boolean pause = false;
    
    onCreate(){
        if (pause) {
            delay = xx + yy;
    else
           delay = xx;
    
    
    Handler.postdelay(new ... , delayTime);
    

    然后显然,在 PostDelay 的内部类中创建 pause = true

    【讨论】:

    • Rusheel:当用户单击按钮时,倒计时开始(10 秒),当完成时,另一个倒计时开始(4 秒)。你能看到我更新的图片吗?
    猜你喜欢
    • 1970-01-01
    • 2015-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多