2013-02-15 132 views
0

基本上我正在做一个有氧运动的功能,并有一个嵌套在一个行内的三个倒计时定时器,所以当一个定时器完成后,下一个定时器开始。准备时间一个,锻炼时间一个,休息时间一个,用户选择这些时间。倒计时器不循环

我需要它循环但用户从numberpicker中选择多次,但无论我做什么,它只会经历一次,不循环,所以我知道它的一切工作,它只是循环的一部分,工作。

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

//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(); 

    } 

回答

0

这里的问题是,您创建prepCountTimer并在完成等指派,然后启动它。然后到达每个的结尾,并再次循环,并开始另一个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(); 

} 
+0

我该怎么做?我不明白为什么它不会根据用户选择的重复x次... – user1875797 2013-02-15 21:08:51

+0

我已经编辑了我的答案,以显示您可能会如何做到这一点。 – Eluvatar 2013-02-15 21:37:46