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