Chronometer is a simple timer. You can give it a start time and a scheduled time, or if you don't give it a start time, it will use your time to start the call. By default, it is displayed in the form of "minute: Second" or "H: mm: SS" in the current timer value, or set (string) that can be used) the format of the timer value to any string
1. Important attributes
Android: Format of the defined time, for example: hh: mm: SS
2. Important Methods
Setbase (long base): sets the countdown timer.
Setformat (string format): Set the display time format.
Start (): start timing
Stop (): Stop timing
Setonchronometerticklistener (chronometer. onchronometerticklistener listener): called when the timer changes
Instance:
<? 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: gravity = "center_horizontal" Android: orientation = "vertical"> <chronometer Android: Id = "@ + ID/chronometer" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: textcolor = "# ffff0000" Android: textsize = "12pt"/> <button Android: Id = "@ + ID/start" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: onclick = "onclick" Android: text = ""/> <button Android: Id = "@ + ID/Stop" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: onclick = "onclick" Android: text = "stop"/> <button Android: id = "@ + ID/RESET" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: onclick = "onclick" Android: TEXT = "reset"/> </linearlayout>
Package COM. light. study. android; import android. app. activity; import android. OS. bundle; import android. OS. systemclock; import android. view. view; import android. widget. button; import android. widget. chronometer; public class mainactivity extends activity {private button startbtn, stopbtn, resetbtn; private chronometer chronomete; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); chronomete = (chronometer) This. findviewbyid (R. id. chronometer); startbtn = (button) This. findviewbyid (R. id. start); stopbtn = (button) This. findviewbyid (R. id. stop); resetbtn = (button) This. findviewbyid (R. id. reset);} public void onclick (view v) {If (V = startbtn) {// start timing chronomete. start ();} else if (V = stopbtn) {chronomete. stop (); // stop timing} else if (V = resetbtn) {chronomete. setbase (systemclock. elapsedrealtime (); // reset }}}
Result: