2010-10-16 1 views
5

http://helloandroidworld.com/2010/02/how-to-create-a-simple-android-countdown-timer/에서 카운트 다운 타이머를 만들려고했지만 문서에 설명 된대로 00:00:00을 원하는 형식으로 작동시키지 못합니다. 타이머가 시작되고 일시 중지 할 수 있으며 2 분 동안 실행되지만 실행되는 것을 볼 수는 없습니다. 일시 중지하면 시간이 얼마나 남았는지 알 수 있지만 작동시킬 수는 없습니다. 서식을 지정하지 않으면 제대로 작동하고 카운트 다운됩니다. 누구나이 작업을하거나 문제를 해결하는 방법을 알고 있습니까? 나는 주위를 검색 해왔고 포맷팅과 함께 이런 식으로 카운트 다운 타이머를 다루는 것을 찾을 수 없다. 어떤 도움을 주시면 감사하겠습니다.카운트 다운 타이머의 서식을 00:00:00으로 설정하지 않았습니다. 00:00:00을 표시하고 일시 중지 할 때까지 남은 시간을 표시합니다.

TextView timeDisplay; 
    MyCount counter; 
    int state = 0; 
    int length = 120000; 
    long startTime = 0; 
    long currentTime = 0; 
    long timeElapsed = 0; 
    long timeRemaining = 0; 
    long prevTimeRemaining = 0; 
    Button control; 

    public String formatTime(long millis) { 
      String output = "00:00:00"; 
      long seconds = millis/1000; 
      long minutes = seconds/60; 
      long hours = minutes/60; 

      seconds = seconds % 60; 
      minutes = minutes % 60; 
      hours = hours % 60; 

      String secondsD = String.valueOf(seconds); 
      String minutesD = String.valueOf(minutes); 
      String hoursD = String.valueOf(hours); 

      if (seconds < 10) 
      secondsD = "0" + seconds; 
      if (minutes < 10) 
      minutesD = "0" + minutes; 
      if (hours < 10) 
      hoursD = "0" + hours; 

      output = hoursD + " : " + minutesD + " : " + secondsD; 
      return output; 
     } 

    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.countdown); 

    timeDisplay = (TextView) findViewById(R.id.timer); 
    control = (Button) findViewById(R.id.control); 
    counter = new MyCount (length, 1000); 
    } 

    public void control(View view) { 
    switch (state) { 
    case 0: 
     startTime = System.currentTimeMillis(); 
     counter.start(); 
     control.setText(R.string.pause); 
     state = 1; 
     break; 
    case 1: 
     // pause 
     currentTime = System.currentTimeMillis(); 
     timeElapsed = currentTime - startTime; 
     if (prevTimeRemaining == 0) 
     timeRemaining = length - timeElapsed; 
     else 
     timeRemaining = prevTimeRemaining - timeElapsed; 
     counter.cancel(); 
     timeDisplay.setText("" + formatTime(timeRemaining)); 
     control.setText(R.string.resume); 
     prevTimeRemaining = timeRemaining; 

     // resume 
     counter = new MyCount(timeRemaining, 1000); 
     state = 0; 
     break; 
    case 2: 
     prevTimeRemaining = 0; 
     counter = new MyCount(length, 1000); 
     control.setText(R.string.start); 
     timeDisplay.setText(R.string.timer); 
     state = 0; 
    } 
    } 

    public class MyCount extends CountDownTimer { 

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

    public void onFinish() { 
     timeDisplay.setText("done!"); 
     state = 2; 
    control.setText(R.string.restart); 
    } 

    public void onTick (long millisUntilFinished) { 
     timeDisplay.setText ("Left: " + formatTime(timeRemaining)); 

    } 

    } 

} 
+2

나는 그것을 얻었다. 원래 timeDisplay.setText ("왼쪽 :"+ formatTime (timeRemaining)); timeDisplay.setText ("Left :"+ millisUntilFinished/100);이었습니다. formatTime을 millisUntilFinished 앞에 아무런 성공없이 넣으려고했으나/100없이 시도했지만 – Daniel

+7

답변을 작성하고 답을 확인하십시오.이 질문은 답변되지 않은 목록에서 제거됩니다. – rds

답변

관련 문제