Simple countdown to Android CountDownTimer

Source: Internet
Author: User

Simple countdown to Android CountDownTimer

CountDownTimer:

Schedule a countdown until a time in the future, with regular communications on intervals along the way.

The countdown class is relatively simple. You can learn how to design the class. Here is an example of a simple application of this class:

Optimization is also required for such a small example. The optimization has not been considered as a result. In simple optimization, the code for clicking Dlialog multiple times is added without re-creating and canceling the countdowntimer task;

After simple optimization:

Package com. example. dliagdemo; import android. app. activity; import android. app. dialog; import android. content. dialogInterface; import android. OS. bundle; import android. OS. countDownTimer; import android. view. layoutInflater; import android. view. view; import android. widget. button; import android. widget. toast; public class MainActivity extends Activity {private Button btnSure; private MainActivity instance; privat E CountDownTimer mDownTimer; private Dialog dialog; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); instance = this; Button btn = (Button) findViewById (R. id. btn); btn. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {if (dialog = null) showDialog (); else if (! Dialog. isShowing () showDialog () ;}});} private void showDialog () {dialog = new Dialog (instance); LayoutInflater inflater = LayoutInflater. from (instance); View v = inflater. inflate (R. layout. itmes, null); btnSure = (Button) v. findViewById (R. id. buttonSure); btnSure. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {Toast. makeText (instance, "Game started", Toast. LENGTH_SHORT ). show (); If (dialog! = Null) {dialog. dismiss () ;}}); mDownTimer = new CountDownTimer (10000,100 0) {public void onTick (long millisUntilFinished) {if (btnSure! = Null) {btnSure. setText ("automatic start (" + millisUntilFinished/1000 + "/s)") ;}} public void onFinish () {if (btnSure! = Null) {btnSure. optional mclick ();}}}. start (); dialog. setContentView (v); dialog. setTitle ("Start game"); dialog. setOnDismissListener (new DialogInterface. onDismissListener () {@ Overridepublic void onDismiss (DialogInterface dialog) {if (mDownTimer! = Null) {mDownTimer. cancel (); mDownTimer = null ;}}); dialog. show ();}}

Only implement countdown.

Package com. example. dliagdemo; import android. app. activity; import android. app. dialog; import android. OS. bundle; import android. OS. countDownTimer; import android. view. layoutInflater; import android. view. view; import android. widget. button; import android. widget. toast; public class MainActivity extends Activity {private Button btnSure; private MainActivity instance; @ Overrideprotected void onCreate (Bundle saved InstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); instance = this; Button btn = (Button) findViewById (R. id. btn); btn. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {showDialog () ;}});} private void showDialog () {final Dialog dialog = new Dialog (instance ); layoutInflater inflater = LayoutInflater. from (instance); View v = inflater. Inflate (R. layout. itmes, null); btnSure = (Button) v. findViewById (R. id. buttonSure); btnSure. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {Toast. makeText (instance, "Game started", Toast. LENGTH_SHORT ). show (); if (dialog! = Null & dialog. isShowing () {dialog. dismiss () ;}}); new CountDownTimer (10000,100 0) {// total time, interval public void onTick (long millisUntilFinished) {btnSure. setText ("coming soon (" + millisUntilFinished/1000 + "/s)");} public void onFinish () {if (dialog! = Null & dialog. isShowing () {btnSure. optional mclick (); // click the dialog button. dismiss ();}}}. start (); dialog. setContentView (v); dialog. setTitle ("Start game"); dialog. show ();}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.