0

基本上我正在做一个有氧运动功能,并且有三个倒数计时器连续嵌套在一起,所以当一个计时器完成时,下一个计时器开始。一种是准备时间,一种是锻炼时间,一种是休息时间,用户可以选择这些时间。

无论用户从数字选择器中选择多少次,我都需要它循环,但无论我做什么,它都只会遍历一次并且不会循环,所以我知道这一切都有效,只是循环部分不起作用。

我在这里错过了什么吗?有一个更好的方法吗?

    //Main countdown timers loop
    for(int i = 0; i <= times.getValue() + 1; i++) //times NumberPicker
    {
         prepCountTimer = new CountDownTimer(_finalPrep * 1000, 1000) {

             public void onTick(long millisUntilFinished) {

                 tvRoundCount.setText("Round " + roundCount + " / " + times.getValue());
                 tvCountDown.setText((millisUntilFinished / 1000) + "s");
                 if(millisUntilFinished <= (6 * 1000))
                 {
                     tvCountDown.setTextColor(Color.RED);
                 }
             }

             public void onFinish() {
                 workoutCountTimer = new CountDownTimer(_finalWorkout * 1000, 1000) {

                     public void onTick(long millisUntilFinished) {
                         tvCountDown.setTextColor(Color.GREEN);
                         tvCountDown.setText((millisUntilFinished / 1000) + "s");
                         if(millisUntilFinished <= 6 * 1000)
                         {
                             tvCountDown.setTextColor(Color.RED);
                         }
                     }

                     public void onFinish() {
                         restCountTimer = new CountDownTimer(_finalRest * 1000, 1000) {

                             public void onTick(long millisUntilFinished) {
                                 tvCountDown.setTextColor(Color.GREEN);
                                 tvCountDown.setText((millisUntilFinished / 1000) + "s");
                                 if(millisUntilFinished <= 6 * 1000)
                                 {
                                     tvCountDown.setTextColor(Color.RED);
                                 }
                             }

                             public void onFinish() {
                                 roundCount = roundCount + 1;
                             }
                          }.start();
                     }
                  }.start();
             }
          }.start();

    }
4

1 回答 1

0

这里的问题是您prepCountTimer在完成等时创建和分配,然后启动它。然后它到达 for each 的末尾并再次循环创建另一个preopCountTimer并启动它。完成后,您需要restCountTimer开始下一个preopCountTimer。除非我在这里理解错误。

public void callingMethod() {
    timerMethod(times.getValue());
    // execution continues as your timer will run in a different thread
}

public void timerMethod(final int count) {
    if (count == 0) {
        // we have done the number of timers we want we can
        // call whatever we wanted to once our timers were done
    }
    //you could use count to get the times for each timer here
    startTimer(_finalPrep, new timerListner() {
        @Override
        public void timerFinish() {
            //when timer 1 finishes we will start timer 2
            startTimer(_finalWorkout, new timerListner() {
                @Override
                public void timerFinish() {
                    //when timer 2 finishes we will start timer 3
                    startTimer(_finalRest, new timerListner() {
                        @Override
                        public void timerFinish() {
                            //when timer 3 finishes we want to call the next timer in the list.
                            timerMethod(count - 1);
                        }
                    });
                }
            });
        }
    });
}

private interface timerListner {
    void timerFinish();
}

public void startTimer(int timerTime, final timerListner onFinish) {
    // you can pass in other parameters unqiue to each timer to this method aswell
    CountDownTimer timer = new CountDownTimer(timerTime * 1000, 1000) {
        public void onTick(long millisUntilFinished) {
            tvRoundCount.setText("Round " + roundCount + " / " + times.getValue());
            tvCountDown.setText((millisUntilFinished / 1000) + "s");
            if (millisUntilFinished <= (6 * 1000)) {
                tvCountDown.setTextColor(Color.RED);
            }
        }

        @Override
        public void onFinish() {
            onFinish.timerFinish();
        }
    };
    timer.start();

}
于 2013-02-15T20:45:56.893 回答