Timer Program, hitting start, timer starts timing, stops automatically after 20 seconds, uses the timer control
Package Org. crazyit. time; 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; import android. widget. chronometer. onchronometerticklistener; public class chronometertest extends activity {@ overridepublic void oncreate (bundle savedinstancestate) {s Uper. oncreate (savedinstancestate); setcontentview (R. layout. main); // obtain the timer component final chronometer CH = (chronometer) findviewbyid (R. id. test); // obtain the start button start = (button) findviewbyid (R. id. start); start. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (View Source) {// set the start time ch. setbase (systemclock. elapsedrealtime (); // start the timer ch. start () ;}}); // timer listener ch. setonchronometerticklistener (New on Chronometerticklistener () {@ overridepublic void onchronometertick (chronometer ch) {// If the timer time has exceeded 20 s from the start. If (systemclock. elapsedrealtime ()-ch. getbase ()> 20*1000) {Ch. Stop ();}}});}}
<?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"android:gravity="center_horizontal">
<! -- Timer control --> <chronometerandroid: Id = "@ + ID/test" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: textsize = "12pt" Android: textcolor = "# ffff0000"/> <buttonandroid: Id = "@ + ID/start" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = "start"/> </linearlayout>