(Transferred from: http://blog.csdn.net/sun6255028/article/details/6688349)
CHRONOMETR is a simple timer, you can give it a start time, and take this timing, or if you don't give it a start time,
It will start with your time call. By default it will display the "minutes: Seconds" or "Mm:ss" in the current timer's worthwhile form,
Or you can use the set (string) timer value to an arbitrary string
Important attributes
1 android:format defines the time format such as: HH:MM:SS
Important method
1 setbase (long Base) // Set countdown timer 2 setformat (String format) // Set the format of the display time 3 start () // start timing 4 Stop () // Stop timer 5 // call when timer changes 6
:
Routine Main.xml source code
1<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"2android:orientation= "Vertical"3Android:layout_width= "Wrap_content"4android:layout_height= "Wrap_content" >5 6<chronometer7Android:id= "@+id/chronometer"8Android:layout_width= "Wrap_content"9android:layout_height= "Wrap_content"/>Ten One<LinearLayout AAndroid:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" > - the<Button -android:onclick= "OnStart" -android:text= "Start timing" -Android:layout_width= "Wrap_content" +android:layout_height= "Wrap_content"/> - +<Button Aandroid:onclick= "OnStop" atandroid:text= "Stop Timing" -Android:layout_width= "Wrap_content" -android:layout_height= "Wrap_content"/> - -<Button -android:onclick= "OnReset" inandroid:text= "Reset" -Android:layout_width= "Wrap_content" toandroid:layout_height= "Wrap_content"/> +</LinearLayout> -</LinearLayout>
Java source Code
ackage Com.sx.Chronometer; Importandroid.app.Activity; ImportAndroid.os.Bundle; ImportAndroid.os.SystemClock; ImportAndroid.view.View; ImportAndroid.widget.Chronometer; Public classChronometeractivityextendsActivity {PrivateChronometer chronometer =NULL; /**Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); Chronometer=(chronometer) Findviewbyid (r.id.chronometer); Chronometer.setformat ("Timing:%s"); } Public voidOnStart (view view) {Chronometer.start (); } Public voidonStop (view view) {chronometer.stop (); } Public voidonreset (view view) {Chronometer.setbase (Systemclock.elapsedrealtime ()); } }
Android Control Development chronometer (GO)