Make a countdown timer from 10 seconds to 1 second

I have a CountDown timer that counts from 10,000 ms to 0 ms in 1 second increments to make the button pressed after 10 seconds. Although the timer is accurate and does what the code says, I would like to change the way seconds are expressed, but I don't know how to do it.

Java:

void startTimer() { cTimer = new CountDownTimer(10000, 1000) { public void onTick(long millisUntilFinished) { c.setText("Please wait " + millisUntilFinished/1000 + " seconds"); thx.setText(millisUntilFinished/1000 + ""); thx.setAlpha(.5f); thx.setClickable(false); } public void onFinish() { c.setText("done"); thx.setText("ready"); thx.setAlpha(1f); thx.setClickable(true); } }; cTimer.start(); } 

It is output (every second): 9, 8, 7, 6, 5, 4, 3, 2, 1, (still 1), ready

Desired: (every second): 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, ready

Thanks,

IN

EDIT:

I added 1 to the countdown, thx.setText(((millisUntilFinished/1000) + 1) + "");

New output: 10, 9, 8, 7, 6, 5, 4 ,3 , 2, (Still 2), ready

Closer ... but not quite.

+5
source share
4 answers

This is just my study of CountDownTimer when I used CountDownTimer a few months before, and its work is great in my application.

 public void onTick(long millisUntilFinished) 

This millisUntilFinished will give the remaining time in milliseconds, and the last 1000 milliseconds should call the onFinish () method. Therefore, the onTick method will be called until the remaining time is longer (1000 (for OnFinish) + 1000 (for the counter)) milliseconds, if the last remaining millisecond is less than 2000 milliseconds, it will skip onTick () and it will directly call onFinish () when the timer ends. See Handler in this source for more information.

Thus, the main problem is when we give some X (our case of 10000) milliseconds, but to start the counter, it takes from 50 to 150 milliseconds. Therefore, if we add this millisecond to our total time, we will get the counter to the end,

So, you can try like this: nothing changes, only I added 150 milliseconds for your total time.

 void startTimer() { cTimer = new CountDownTimer(10150, 1000) { public void onTick(long millisUntilFinished) { c.setText("Please wait " + millisUntilFinished/1000 + " seconds"); thx.setText(millisUntilFinished/1000 + ""); thx.setAlpha(.5f); thx.setClickable(false); } public void onFinish() { c.setText("done"); thx.setText("ready"); thx.setAlpha(1f); thx.setClickable(true); } }; cTimer.start(); } 

Let me know if this works or not. If you ask me why you are not using Handler, I can tell CountDownTime internally using a handler.

0
source

Try this for a counter:

  public void CountDown() { final TextView textic = (TextView) findViewById(R.id.tvConuter); CountDownTimer Count = new CountDownTimer(10000, 1000) { public void onTick(long millisUntilFinished) { long str = millisUntilFinished / 1000; String TimeFinished = String.valueOf(str); textic.setText(TimeFinished); } public void onFinish() { textic.setText("STOP"); } }; Count.start(); } 

This worked great for me.

+1
source

I would suggest using the Timer() or Handler() class.

 int tick = 0; Handler handler = new Handler(); Runnable r = new Runnable() {void run(){ if (i < 10) { handler.postDelayed()} else { //finished tick = 0; }}}; 

Starting:

handler.post(r);

0
source
 new CountDownTimer(10000, 1000) { public void onTick(long millisUntilFinished) { mTextField.setText("seconds: " + millisUntilFinished / 1000); //here you can have your logic to set text to edittext } public void onFinish() { mTextField.setText("done!"); } }.start(); 
0
source

Source: https://habr.com/ru/post/1268761/


All Articles