开发者

How can I allow my CountDownTimer to reset after the countdown is finished?

开发者 https://www.devze.com 2023-02-06 18:48 出处:网络
Hey all, I\'m trying to get my CountDownTimer to autoreset back to it\'s original time after the time reaches zero. Like an infinite loop. Here is the code I have so far (there are 3 timers in total):

Hey all, I'm trying to get my CountDownTimer to autoreset back to it's original time after the time reaches zero. Like an infinite loop. Here is the code I have so far (there are 3 timers in total):

package com.android.countdown;


import android.app.Activity;
import 开发者_开发知识库android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;

public class countdown extends Activity {


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

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

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

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

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

   @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

//Declare Start/Stop button
Button btnstart = (Button)findViewById(R.id.btnstart);
Button btnstop = (Button)findViewById(R.id.btnstop);

//Declare Text fields to show time left
final TextView mCounter1TextField=(TextView)findViewById(R.id.counter1);
final TextView mCounter2TextField = (TextView)findViewById(R.id.counter2);
final TextView mCounter3TextField=(TextView)findViewById(R.id.counter3);



//Counter 1
final CountDownTimer Counter1 = new CountDownTimer(120000 , 1000) {
public void onTick(long millisUntilFinished) {
    mCounter1TextField.setText("Seconds left: " + formatTime(millisUntilFinished));
}

public void onFinish() {
    mCounter1TextField.setText("Finished!");
}
};

//Counter 2
final CountDownTimer Counter2 = new CountDownTimer(80000 , 1000) {
 public void onTick(long millisUntilFinished) {
     mCounter2TextField.setText("Seconds left: " + formatTime(millisUntilFinished));
 }

 public void onFinish() {
     mCounter2TextField.setText("Finished!");
 }
 };

//Counter 3
final CountDownTimer Counter3 = new CountDownTimer(10000 , 1000) {
  public void onTick(long millisUntilFinished) {
      mCounter3TextField.setText("Seconds left: " + formatTime(millisUntilFinished));
  }

  public void onFinish() {
      mCounter3TextField.setText("Finished!");
  }
  };


//Start Button
btnstart.setOnClickListener(new OnClickListener() {
 public void onClick(View v) {
  Counter1.start();
  Counter2.start();
  Counter3.start();
   }
});

//Stop Button
btnstop.setOnClickListener(new OnClickListener() {
 public void onClick(View v) {
  Counter1.cancel();
  Counter2.cancel();
  Counter3.cancel();
    }
});
}
}

I can't figure what to put inside onFinish()


Put the CounterX.start() in the respective onFinish()

//Counter 1 
final CountDownTimer Counter1 = new CountDownTimer(120000 , 1000) { 
public void onTick(long millisUntilFinished) {
        mCounter1TextField.setText("Seconds left: " + formatTime(millisUntilFinished)); 
}

public void onFinish() {
    mCounter1TextField.setText("Finished!");
    Counter1.start(); 
}
};
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号