Learn android from scratch (chronometer timer component .)

Source: Internet
Author: User

Learn android from scratch (chronometer timer component .)
The timer is used to calculate the time consumed by each athlete. In the Android system, the Chronometer component can be used for this timer function. The Inheritance structure of this class is as follows: 2java. lang. Object? Android. view. View? Android. widget. TextView? Android. widget. Chronometer
Common Methods

1 Public Chronometer (Context context) Structure Create a Chronometer object
2 Public long getBase () Normal Set a benchmark time.
3 Public void setFormat (String format) Normal Set the display format
4 Public long getBase () Normal Returns the set benchmark time.
5 Public String getFormat () Normal Return to the set display format
6 Public void start () Normal Start timing
7 Public void stop () Normal Stop timing
8 Public void setOnChronometerTickListener (Chronometer. OnChronometerTickListener listener) Normal Set listener events for timing changes


Xml file
     
      
      
      
      
  
 

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);} class ButOperator implements OnClickListener {@ Overridepublic void onClick (View view) {// TODO Auto-generated method stubswitch (view. getId () {case R. id. button1: // start the timer MainActivity. this. chronometer. start (); break; case R. id. button2: // stop the timer MainActivity. this. chronometer. stop (); break; case R. id. button3: // reset the parameter MainActivity. this. chronometer. setBase (SystemClock. elapsedRealtime (); break; case R. id. button4: // format the data MainActivity. this. chronometer. setFormat ("current date: % s"); break; default: break ;}}}}


Next prediction: SlidingDrawer drawer component


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.