Android Countdowntimer countdown Simple to use

Source: Internet
Author: User

Countdowntimer:

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

Countdown This class is relatively simple, you can learn to design the class, below see a simple application of this class example:

This small example also needs to be optimized, the optimization is not considered to have only achieved the effect, simple optimization under the addition of multiple clicks Dlialog do not repeat the creation and cancellation of Countdowntimer task code;

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;private countdowntimer m Downtimer;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 start", Toast.length_short). Show (); if (dialog! = null) {Dialog.dismiss ();}}); Mdowntimer = new Countdowntimer (10000, +) {public void OnTick (long millisuntilfinished) {if (btnsure! = null) {btnsure. SetText ("Auto Start (" + millisuntilfinished/1000+ "/s)");}} public void OnFinish () {if (btnsure! = null) {Btnsure.performclick ();}}}. Start ();d Ialog.setcontentview (v);d ialog.settitle ("Start Game");d Ialog.setondismisslistener (new Dialoginterface.ondismisslistener () {@Overridepublic void Ondismiss (Dialoginterface dialog) {if (Mdowntimer! = null) { Mdowntimer.cancel (); Mdowntimer = null;}}); Dialog.show ();}}

Simply achieve the countdown effect.

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 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) {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 start", Toast.length_short). Show (); if (dialog!=null&&dialog.isshowing ()) {Dialog.dismiss ();}}); New Countdowntimer (10000, 1000) {//Total time, interval public void OnTick (long millisuntilfinished) {Btnsure.settext ("Start soon" + millisuntilfinished/1000+ "/s)");} public void OnFinish () {if (dialog!=null&&dialog.isshowing ()) {Btnsure.performclick ();//Click the button Dialog.dismiss ( );}}}. Start ();  Dialog.setcontentview (v);d ialog.settitle ("Start Game");d ialog.show ();} }

Android Countdowntimer countdown Simple to use

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.