For more information, see:
Android timer chronometer basic use: http://bbs.chinaunix.net/archiver? Tid-1329164.html
Chistmmeter. xml:
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Chronometer Android: text = "chronometer"
Android: Id = "@ + ID/chronometer"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
<Button Android: text = "start"
Android: Id = "@ + ID/start"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
<Button Android: text = "stop"
Android: Id = "@ + ID/stop"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
<Button Android: text = "reset"
Android: Id = "@ + ID/RESET"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
<Button Android: text = "set_format"
Android: Id = "@ + ID/set_format"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
<Button Android: text = "clear_format"
Android: Id = "@ + ID/clear_format"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
</Linearlayout>
------------------------------------------------------------------------------------------------------------ Chronometerdemo. java: Package COM. example. android. APIS. view; import android. app. activity; import android. OS. bundle; import android. OS. systemclock; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. chronometer; public class chronometerdemo extends activity {chronometer mchronometer; @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. chronometer); button; mchronometer = (chronometer) findviewbyid (R. id. chronometer); // watch for button clicks. button = (button) findviewbyid (R. id. start); button. setonclicklistener (mstartlistener); // corresponding to start timing button = (button) findviewbyid (R. id. stop); button. setonclicklistener (mstoplistener); // corresponding to the stop timing button = (button) findviewbyid (R. id. reset); button. setonclicklistener (mresetlistener); // corresponding to the reset number button = (button) findviewbyid (R. id. set_format); button. setonclicklistener (msetformatlistener); // The display format of the set time is button = (button) findviewbyid (R. id. clear_format); button. setonclicklistener (mclearformatlistener); // This parameter corresponds to a non-formatted timing display function.} view. onclicklistener mstartlistener = new onclicklistener () {public void onclick (view v) {mchronometer. start () ;}}; view. onclicklistener mstoplistener = new onclicklistener () {public void onclick (view v) {mchronometer. stop () ;}}; view. onclicklistener mresetlistener = new onclicklistener () {public void onclick (view v) {mchronometer. setbase (systemclock. elapsedrealtime () ;}}; view. onclicklistener msetformatlistener = new onclicklistener () {public void onclick (view v) {mchronometer. setformat ("formatted time (% s)") ;}}; view. onclicklistener mclearformatlistener = new onclicklistener () {public void onclick (view v) {mchronometer. setformat (null );}};}