Countdown using digital controls

Source: Internet
Author: User
Tags set time

 

 

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: gravity = "center_horizontal" Android: orientation = "vertical"> <linearlayout Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Orientation = "horizontal" Android: gravity = "center_horizontal: paddingleft = "3dp"> <numberpicker Android: Id = "@ + ID/Np1" Android: layout_width = "50dp" Android: layout_height = "wrap_content"/> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_gravity = "center_vertical | center_horizontal" Android: textsize = "20sp" Android: text = ": "/> <numberpicker Android: Id =" @ + ID/NP2 "Android: layout_width =" 50dp "Android: layout_height =" wrap_content "Android: layout_marginleft = "5dip"/> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_gravity = "center_vertical | center_horizontal" Android: textsize = "20sp" Android: text = ":"/> <numberpicker Android: Id = "@ + ID/np3" Android: layout_width = "50dp" Android: layout_height = "wrap_content"/> </linearlayout> <textview Android: Id = "@ + ID/TXT" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"/> <linearlayout Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: Orientation = "horizontal"> <textview Android: id = "@ + ID/showtiqian" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"/> </linearlayout> <linearlayout Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: Orientation = "horizontal"> <button Android: Id = "@ + ID/btn_start" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "Start countdown"/> <button Android: Id = "@ + ID/button1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "stop"/> <textview Android: Id = "@ + ID/showcount" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"/> </linearlayout>

  

Package COM. example. activity; import Java. util. calendar; import android. annotation. suppresslint; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. numberpicker; import android. widget. numberpicker. onvaluechangelistener; import android. widget. textview; import com. Example. test. r; public class timecountactivity extends activity {private Boolean stop = true; private numberpicker msecondspinner; private numberpicker mhourspinner; private numberpicker extends; private textview timecount; private button btn_start, btn1; private calendar mdate; private int mhour, mminute, msecond; private handler mhandler = new handler (); // global handlerint time = 0; // time difference @ suppresslint ("Newapi") protected void oncreate (bundle savedinstancestate) {setcontentview (R. layout. timecount); super. oncreate (savedinstancestate); mdate = calendar. getinstance (); mhour = mdate. get (calendar. hour_of_day); mminute = mdate. get (calendar. minute); msecond = mdate. get (calendar. second); mhourspinner = (numberpicker) This. findviewbyid (R. id. np1); mhourspinner. setmaxvalue (23); mhourspinner. setminvalue (0); mhourspin Ner. setvalue (mhour); mhourspinner. setonvaluechangedlistener (monhourchangedlistener); mminutespinner = (numberpicker) This. findviewbyid (R. id. NP2); mminutespinner. setmaxvalue (59); mminutespinner. setminvalue (0); mminutespinner. setvalue (mminute); mminutespinner. setonvaluechangedlistener (monminutechangedlistener); msecondspinner = (numberpicker) This. findviewbyid (R. id. np3); msecondspinner. setmaxvalue (59); mseco Ndspinner. setminvalue (0); msecondspinner. setvalue (msecond); msecondspinner. setonvaluechangedlistener (mondatechangedlistener); timecount = (textview) This. findviewbyid (R. id. showcount); btn_start = (button) This. findviewbyid (R. id. btn_start); btn_start.setonclicklistener (New mclick (); btn1 = (button) This. findviewbyid (R. id. button1); btn1.setonclicklistener (New mclick ();} class mclick implements onclicklist Ener {public void onclick (view v) {If (V = btn_start) {stop = false; time = msecond + mminute * 60 + mhour * 3600; new thread (New timecount ()). start (); // enable thread} else if (V = btn1) {stop = true ;}}@ suppresslint ("newapi") Private numberpicker. onvaluechangelistener mondatechangedlistener = new onvaluechangelistener () {@ suppresslint ("newapi") Public void onvaluechange (numberpicker Picker, int oldval, int newval) {m Second = mhourspinner. getvalue () ;};@ suppresslint ("newapi") Private numberpicker. onvaluechangelistener listener = new onvaluechangelistener () {@ suppresslint ("newapi") Public void onvaluechange (numberpicker Picker, int oldval, int newval) {mhour = mhourspinner. getvalue () ;};@ suppresslint ("newapi") Private numberpicker. onvaluechangelistener monminutechangedlistener = new onvaluechangelisten Er () {@ suppresslint ("newapi") Public void onvaluechange (numberpicker Picker, int oldval, int newval) {mminute = mminutespinner. getvalue () ;}}; class timecount implements runnable {@ overridepublic void run () {While (! Stop & time> 0) // the cycle of the entire countdown execution {time --; mhandler. post (New runnable () // use it to modify the remaining time displayed in the UI main thread {public void run () {timecount. settext (getinterval (time); // display the remaining time}); try {thread. sleep (1000); // thread sleep for one second. This is the countdown interval} catch (interruptedexception e) {e. printstacktrace () ;}/// below is the countdown end logic mhandler. post (New runnable () {@ overridepublic void run () {timecount. settext ("the set time is up. ") ;}}}}/ *** Set the display text */public static string getinterval (INT time) {string TXT = NULL; If (Time> = 0) {long hour = time % (24*3600)/3600; // hour long minute = time % 3600/60; // minute long second = time % 60; // second TXT = "still available:" + hour + "Hour" + minute + "Minute" + second + "second ";} else {TXT = "expired";} return TXT ;}}

  

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.