2013-02-15 168 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