Android: countdown timer, for example. 10:00 to 00:00? using OnclickListener for TextView?

I'm trying to make a countdown timer that starts after 10 minutes, looks like a basketball scoreboard: from 10:00 to 00:00. How can I do it? This is my code:

private TextView Timer; Handler handler = new Handler(); private int length = 120000; private int decision = 0; MyCount counter; public String formatTime(long millis) { String output = "00:00"; long seconds = millis / 1000; long minutes = seconds / 60; seconds = seconds % 60; minutes = minutes % 60; String sec = String.valueOf(seconds); String min = String.valueOf(minutes); if (seconds < 10) sec = "0" + seconds; if (minutes < 10) min= "0" + minutes; output = min + " : " + sec; return output; }//formatTime @Override public void onCreate(Bundle cute) { super.onCreate(cute); counter = new MyCount(length, 1000); updateTime(); handler.removeCallbacks(updateTimeTask); handler.postDelayed(updateTimeTask, 1000); }//end of cuteness private Runnable updateTimeTask = new Runnable() { public void run() { updateTime(); handler.postDelayed(this, 1000); } }; private void updateTime() { switch (decision) { case 0: startTime = 0L; counter.start(); decision=1; break; case 1: counter.onPause(); decision=0; break; } }//updateTime class MyCount extends CountDownTimer { public MyCount(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); }//MyCount public void onPause() { //do stuff later onPause(); }//finish public void onTick(long millisUntilFinished) { Timer.setText("" + formatTime(millisUntilFinished)); }//on tick @Override public void onFinish() { onStop(); }//finish }//class MyCount 

Any help would be greatly appreciated. thank you

+6
source share
3 answers

It should not be too complicated. You have already created your functionality for writing your time in letters, now you need to pay attention. Starting the timer is simple, just do it in the start button's event handler (or whatever you decide to use) (modified example from the Android developer link

 // New timer for 10 minutes, starts after initialization new MyCount(600000, 1000) { // Updates the text on your "scoreboard" every second public void onTick(long millisUntilFinished) { Timer.setText("Time remaining: " + formatTime(millisUntilFinished)); } public void onFinish() { mTextField.setText("done!"); } }.start(); 

And that’s all you need! You can skip the UpdateTime function and your updateTimeTask . Just replace all of this with your onCreate method

 counter = new MyCount(length, 1000); updateTime(); handler.removeCallbacks(updateTimeTask); handler.postDelayed(updateTimeTask, 1000); 

With my code. Or change it as you please!

+6
source

why don't you just create 3 text views to make it easier for you to code?

one in minutes.
one for the colon.
one in seconds.

then use the code that it uses.

hope i helped.

+1
source

The way I think of counting down simply saves at the beginning (current time + 10 minutes) to the side, and then simply subtracts it from the current time every second in your handler and displays the result in the desired format.

0
source

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


All Articles