A Timer control (chronometer) is a document that shows how much time has elapsed since a certain starting time.
Inherit from TextView, display time content as text
There are five methods of this component
1, setbase (): \ \ To set the start time of the timer
2, SetFormat (): \ \ For setting the display time format
3. Start (): \ \ For specifying timing start
4. Stop (): \ \ is used to specify stop timing
5. Setonchronometerticklistener (): \ \ is used to bind the event listener to the timer and trigger the listener when the timer changes
Using the example
1, first look at the layout file
1<?xml version="1.0"encoding="Utf-8"?>2<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"3Android:layout_width="match_parent"4android:layout_height="match_parent"5android:orientation="Vertical">6 7 8<chronometer9Android:layout_width="wrap_content"Tenandroid:layout_height="wrap_content" OneAndroid:textsize="20DP" Aandroid:text="time used: 0:00" -android:layout_gravity="Center" -Android:id="@+id/id_jishiqi"/> the -<Button -Android:id="@+id/btn_jishi_1" -Android:layout_width="wrap_content" +android:layout_height="wrap_content" -android:text="Start Timing"/> + A<Button atAndroid:id="@+id/btn_jishi_2" -Android:layout_width="wrap_content" -android:layout_height="wrap_content" -android:text="Pause Timings"/> - -<Button inAndroid:id="@+id/btn_jishi_3" -Android:layout_width="wrap_content" toandroid:layout_height="wrap_content" +android:text="Reset"/> - the</LinearLayout>
Layout:
Look at the Java file again
1 Package base_ui;2 3 import COM.EXAMPLE.ALLCODE.R;4 5 import android.app.Activity;6 import Android.os.Bundle;7 import Android.os.SystemClock;8 import Android.view.View;9 import Android.view.View.OnClickListener;Ten import Android.widget.Button; One import Android.widget.Chronometer; A import Android.widget.Chronometer.OnChronometerTickListener; - - Public classUi_jishiqi extends Activity implements onclicklistener{ the Privatechronometer Jishiqi; - PrivateButton btn_jishi_1; - PrivateButton btn_jishi_2; - PrivateButton btn_jishi_3; + - @Override + protected voidonCreate (Bundle savedinstancestate) { A //TODO auto-generated Method Stub at super.oncreate (savedinstancestate); - Setcontentview (R.layout.base_ui_jishiqi); - -Jishiqi =(chronometer) Findviewbyid (R.id.id_jishiqi); -Btn_jishi_1 =(Button) Findviewbyid (r.id.btn_jishi_1); -Btn_jishi_2 =(Button) Findviewbyid (r.id.btn_jishi_2); inBtn_jishi_3 =(Button) Findviewbyid (r.id.btn_jishi_3); - toJishiqi.setbase (Systemclock.elapsedrealtime ());//set start time, here is starting from 0 +Jishiqi.setformat ("time used:%s"); - theBtn_jishi_1.setonclicklistener ( This); *Btn_jishi_2.setonclicklistener ( This); $Btn_jishi_3.setonclicklistener ( This);Panax NotoginsengHere is the timer's listener, which can add events such as how many events are timed to indicate what text -Jishiqi.setonchronometerticklistener (NewOnchronometerticklistener () { the + @Override A Public voidOnchronometertick (chronometer chronometer) { the //TODO auto-generated Method Stub + - } $ }); $ - - } theResponse Events for three buttons - @OverrideWuyi Public voidOnClick (View v) { the //TODO auto-generated Method Stub - Switch(V.getid ()) Wu { - Caser.id.btn_jishi_1: AboutJishiqi.start ();//Start Timing $ Break; - Caser.id.btn_jishi_2: -Jishiqi.stop ();//Pause Timings - Break; A CaseR.id.btn_jishi_3: +Jishiqi.setbase (Systemclock.elapsedrealtime ());//Reset the Break; - } $ } the the}
The code is simple, and the annotations are clear.
Note that when the Start Timer button is clicked, the timer starts, then clicks the Pause Timer button, the timer pauses, but clicks the start time, the timer does not start the timer from the moment you display the pause, but
Skipping over time, this is the time you click the Pause Timer button and the second click to start the Timer button, which is always calculated.
To start with the time you pause the timer display, you can calculate the time between clicking the Pause Timer button and the second click to start the Timer button minus the time.
:
Simple use of the Android Development _ Timer (chronometer)