How do I stop a timer that hasn't finished yet and then start a new one?

I am trying to make a guessing game. The problem is that my timer expires the next after answering the question (button pressed) and a new timer starts. This causes the two timers to change the text representation at different intervals, which is not how it should be. I would like to know how to stop my previous countdown and start a new one. Thank you! Here's my code:

button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
 final TextView textic = (TextView) findViewById(R.id.button1);
                                long total = 30000;
            final CountDownTimer Count = new CountDownTimer(total, 1000) {

                public void onTick(long millisUntilFinished) {
                    textic.setText("Time Left: " + millisUntilFinished / 1000);
                }
                public void onFinish() {
                    textic.setText("OUT OF TIME!");
                    finish();
                }
                }; 
                Count.start();

      

+3


source to share


2 answers


Didn't test the code, but I would use something like this:



 final TextView textic = (TextView) findViewById(R.id.button1);
 final android.os.CountDownTimer Count = new android.os.CountDownTimer(total, 1000) {
       public void onTick(long millisUntilFinished) {
           textic.setText("Time Left: " + millisUntilFinished / 1000);
       }
       public void onFinish() {
           textic.setText("OUT OF TIME!");
       }
 }; 
 button.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
          Count.cancel();
          Count.start();
      }
   });

      

+4


source


end CountDownTimer in Done Mode



    @Override
    public void onFinish() {
        Log.v(TAG, "On Finish");
        textvieew.setText("your text");
        countDownTimer.cancel();

    }

      

0


source







All Articles