Learn the Android<chronometer Timer component from scratch. 32 .>

Source: Internet
Author: User
Tags getbase

Timers to calculate the time spent by each athlete, and in the Android system, this chronograph function can be used with the chronometer component, the inheritance structure of this class is as follows:2Java.lang.Object?Android.view.View?Android.widget.TextView?Android.widget.Chronometer
a common approach
/tr>
1 public Chr Onometer (Context context) construct Create chronometer object
2 public long GetBase () normal sets a base time that can be done by completing the
3 public void SetFormat (String format) normal setting display format
4 public Long getbase () General return the set base time
5 public String getFormat () normal returns the display of the setting Format
6 public void Start () normal start timing
7 public void Stop () normal stop timing
8 public void set Onchronometerticklistener (Chronometer.onchronometerticklistener listener) normal setting a listener event for timing change


XML File
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <button android:id=" @+id/button1 "style="? Android:attr/buttonstylesmall "and        Roid:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignparentleft= "true"        Android:layout_centervertical= "true" android:text= "Start"/> <button android:id= "@+id/button3" Style= "? Android:attr/buttonstylesmall" Android:layout_width= "wrap_content" android:layout_height= "Wra P_content "Android:layout_centervertical=" TruE "android:layout_torightof=" @+id/button2 "android:text=" reset "/> <button android:id=" @+id/but Ton2 "style="? Android:attr/buttonstylesmall "Android:layout_width=" Wrap_content "Android:layout_heigh t= "Wrap_content" android:layout_centervertical= "true" android:layout_torightof= "@+id/button1" Android : text= "Stop"/> <button android:id= "@+id/button4" style= "? Android:attr/buttonstylesmall" Andro        Id:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignparentright= "true" Android:layout_centervertical= "true" android:layout_marginright= "24DP" android:text= "formatted display"/> & Lt Chronometer android:id= "@+id/chronometer1" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_above= "@+id/button3" android:layout_alignleft= "@+id/button3" Android:la Yout_marginbottom= "52DP "android:textsize=" 25DP "android:text=" chronometer "/></relativelayout> 

Java files
Package Com.example.chronometer;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 Mainactivity extends Activity {public Button start;public button stop;public Button Reset;public button format;public chronometer chronometer; Butoperator operator = new Butoperator (), @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); start = (Button) This.findviewbyid (r.id.button1); stop = (button) This.findviewbyid (r.id.button2); reset = (Button) This.findviewbyid (r.id.button3); format = (button) This.findviewbyid (R.id.button4); chronometer= (chronometer) This.findviewbyid (r.id.chronometer1); Start.setonclicklistener (This.operator); Stop.setonclicklistener (This.operator); Reset.setonclicklistener ( This.operator); Format.setonclicklistener (this.operator);} ClassButoperator implements Onclicklistener {@Overridepublic void OnClick (view view) {//TODO auto-generated method Stubswitch (View.getid ()) {Case r.id.button1://starts timing MainActivity.this.chronometer.start (); Break;case r.id.button2:// Stop timing MainActivity.this.chronometer.stop (); break;case r.id.button3://reset Parameter MainActivity.this.chronometer.setBase ( Systemclock.elapsedrealtime ()); Break;case r.id.button4://formatted Data MainActivity.this.chronometer.setFormat ("Current date:%s" ); break;default:break;}}}


Forecast for next section: Slidingdrawer drawer assembly


Learn the Android<chronometer Timer component from scratch. 32 .>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.