简体   繁体   English

暂停后继续倒数计时器

[英]Continue countdown timer after pause

I have a countdown timer, I have a button that pause it, but I need that when you click on button, continue to countdown. 我有一个倒数计时器,我有一个暂停它的按钮,但我需要当你点击按钮,继续倒计时。 I search but couldn't a function related this. 我搜索但不能与此相关的功能。 How can do it? 怎么办? This is my code, I only managed to restart it, but not continue: 这是我的代码,我只是设法重启它,但没有继续:

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

          }
         }

I thought about taking millisUntilFinished to a variable, but didn't work. 我考虑过将millisUntilFinished用于变量,但是没有用。 Anyway I guess the way is close to that. 无论如何,我猜这种方式接近于此。

You can try saving the seconds until finish, and then you can start the new countdown timer with that seconds. 您可以尝试保存秒数直到完成,然后您可以使用该秒启动新的倒数计时器。

// ----------------------- // -----------------------

Cuando presionas el boton de pausa, guarda los segundos que le faltan al timer para que termine. Cuando presionas el boton de pausa,guarda los segundos que le faltan al timer para que termine。 Entonces, cuando volves a apretar play, creas un nuevo CountDownTimer con esos segundos que te faltaban. Entonces,cuando volve a apretar play,creas un nuevo CountDownTimer con esos segundos que te faltaban。

UPDATE UPDATE

I did an example: 我做了一个例子:

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

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM