This is a small example of learning the chronometer control. paste your own code and communicate with you.
Effect:
Implementation ideas:
Use the chronometer control to operate the meter. Set the initial time by setting setbase (long base), and then add a setonchronometerticklistener (chronometer. onchronometerticklistener L) event to determine whether the time has arrived, and then call its stop () method to stop timing.
Implementation Code:
Main. xml:
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Background = "@ drawable/back" Android: gravity = "center" Android: Orientation = "vertical"> <linearlayout Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: layout_margin = "10dip" Android: orientation = "horizontal"> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: layout_weight = "4" Android: gravity = "center" Android: TEXT = "set time:"/> <edittext Android: Id = "@ + ID/edt_settime" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: layout_weight = "1" Android: inputtype = "Number"/> </linearlayout> <chronometer Android: Id = "@ + ID/chronometer" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: gravity = "center" Android: textcolor = "# ff0000" Android: textsize = "60dip"/> <linearlayout Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: layout_margin = "10dip" Android: Orientation = "horizontal"> <button Android: Id = "@ + ID/btnstart" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: layout_weight = "1" Android: text = "Start recording"/> <button Android: id = "@ + ID/btnstop" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: layout_weight = "1" Android: TEXT = "stop recording"/> <button Android: Id = "@ + ID/btnreset" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: layout_weight = "1" Android: text = "reset"/> </linearlayout>
Activity Code:
Package COM. jiahui. chronometer; import android. app. activity; import android. app. alertdialog; import android. app. dialog; import android. content. dialoginterface; import android. OS. bundle; import android. OS. systemclock; import android. text. format. time; import android. view. view; import android. widget. button; import android. widget. chronometer; import android. widget. edittext; public class chronometerdemoactivity ext Ends activity {private int starttime = 0; Public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Final chronometer = (chronometer) findviewbyid (R. id. chronometer); button btnstart = (button) findviewbyid (R. id. btnstart); button btnstop = (button) findviewbyid (R. id. btnstop); button btnrest = (button) findviewbyid (R. id. btnreset); Final Edittext edtsettime = (edittext) findviewbyid (R. id. edt_settime); btnstart. setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {system. out. println ("-- start time ---"); string Ss = edtsettime. gettext (). tostring (); If (! (Ss. Equals ("") & SS! = NULL) {starttime = integer. parseint (edtsettime. gettext (). tostring ();} // sets the start time of the lecture chronometer. setbase (systemclock. elapsedrealtime (); // start time chronometer. start () ;}}); btnstop. setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {// stop chronometer. stop () ;}}); // reset btnrest. setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {chronometer. setbase (systemclock. elapsedrealtime () ;}}); chronometer. setonchronometerticklistener (New chronometer. onchronometerticklistener () {@ overridepublic void onchronometertick (chronometer) {// If the start time has exceeded startime seconds now if (systemclock. elapsedrealtime ()-chronometer. getbase ()> starttime * 1000) {chronometer. stop (); // notify the user of showdialog () ;}});} protected void showdialog () {alertdialog. builder = new alertdialog. builder (this); builder. seticon (R. drawable. eb28d25); builder. settitle ("warning "). setmessage ("time "). setpositivebutton ("OK", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {}}); alertdialog dialog = builder. create (); dialog. show ();}}
Problems found:I don't know how to set the time to minute: Second: millisecond. I don't know how to set it in the setformat method. If you want to know it, I will improve the program. Thank you.
If reprint please declare the Article Source: http://blog.csdn.net/jiahui524
Source code download: http://download.csdn.net/detail/jiahui524/3786779