我有一个倒数计时器,我有一个暂停它的按钮,但是当你点击按钮时我需要它,继续倒计时。我搜索但不能与此相关的功能。怎么办?这是我的代码,我只是设法重启它,但没有继续:
private TextView cuentaRegresiva;
private Button btnEmpezar;
private CountDownTimer countDownTimer;
private boolean timerHasStarted = false;
private long startTime = 30 * 1000;
private final long interval = 1 * 1000;
private long restante;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
btnEmpezar.setOnClickListener(iniciar);
}
OnClickListener iniciar=new OnClickListener() {
@Override
public void onClick(View arg0) {
if (!timerHasStarted && !pausado) {
countDownTimer.start();
timerHasStarted = true;
btnEmpezar.setText("Pause");
pausado=false;
}
else if(timerHasStarted && !pausado){
countDownTimer.cancel();
timerHasStarted = false;
btnEmpezar.setText("Restart");
pausado=true;
}
else if(!timerHasStarted && pausado){
countDownTimer2.start();
timerHasStarted = true;
btnEmpezar.setText("Pause");
pausado=false;
}
}
};
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
cuentaRegresiva.setText("Tiempo!");
}
@Override
public void onTick(long millisUntilFinished) {
cuentaRegresiva.setText("" + millisUntilFinished / 1000);
}
}
public class MyCountDownTimer2 extends CountDownTimer {
public MyCountDownTimer2(long restante, long interval) {
super(restante, interval);
}
@Override
public void onFinish() {
cuentaRegresiva.setText("Tiempo!");
}
@Override
public void onTick(long millisUntilFinished) {
cuentaRegresiva.setText("" + millisUntilFinished / 1000);
}
}
我考虑过将millisUntilFinished用于变量,但是没有用。无论如何,我猜这种方式接近于此。
答案 0 :(得分:3)
您可以尝试保存秒数直至完成,然后您可以使用该秒数启动新的倒数计时器。
// -----------------------
Cuando presionas el boton de pausa,guarda los segundos que le faltan al timer para que termine。 Entonces,cuando volve a apretar play,creas un nuevo CountDownTimer con esos segundos que te faltaban。
更新
我做了一个例子:
public class MainActivity extends Activity {
private static final int TIMER_TIME = 10000; // in millis
private Button btnCountdown;
private TextView tvTimeUntilFinish;
private boolean mIsPaused = true;
private long mMillisUntilFinish;
private CountDownTimer mTimer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMillisUntilFinish = TIMER_TIME;
btnCountdown = (Button) findViewById(R.id.btnCountdown);
tvTimeUntilFinish = (TextView) findViewById(R.id.tvTimeUntilFinish);
btnCountdown.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (mIsPaused) {
btnCountdown.setText("Pause");
initTimer();
} else {
btnCountdown.setText("Play");
cancelTimer();
}
mIsPaused = !mIsPaused;
}
});
}
private void cancelTimer() {
if (mTimer != null) {
mTimer.cancel();
mTimer = null;
}
}
private void initTimer() {
mTimer = new CountDownTimer(mMillisUntilFinish, 1000) {
public void onTick(long millisUntilFinished) {
tvTimeUntilFinish.setText("seconds remaining: " + millisUntilFinished / 1000);
mMillisUntilFinish = millisUntilFinished;
}
public void onFinish() {
}
}.start();
}
}