[Chronometer 特點]當 Chronometer 開始計時後,即使按<暫停>時仍會繼續計時。
<ChronometerClock.java>
1: package com.android.chronometerclock;
2:
3: import android.app.Activity;
4: import android.os.Bundle;
5: import android.os.SystemClock;
6: import android.view.View;
7: import android.view.View.OnClickListener;
8: import android.widget.Button;
9: import android.widget.Chronometer;
10: import android.widget.Chronometer.OnChronometerTickListener;
11:
12: public class ChronometerClock extends Activity
13: implements OnClickListener, OnChronometerTickListener
14: {
15: private Chronometer chronometer;
16: private Button btnStart, btnStop, btnReset, btnExit;
17:
18: public void onClick(View view) {
19: switch (view.getId())
20: {
21: case R.id.startBtn:
22: chronometer.start();
23: break;
24: case R.id.stopBtn:
25: chronometer.stop();
26: break;
27: case R.id.resetBtn:
28: chronometer.setBase(SystemClock.elapsedRealtime());
29: break;
30: case R.id.exitBtn:
31: android.os.Process.killProcess(android.os.Process.myPid());
32: break;
33: }
34: }
35:
36: public void onChronometerTick(Chronometer chronometer) {
37:
38: }
39:
40:
41: /** Called when the activity is first created. */
42: @Override
43: public void onCreate(Bundle savedInstanceState) {
44: super.onCreate(savedInstanceState);
45: setContentView(R.layout.main);
46: findViews();
47: setListeners();
48: chronometer.setFormat("Chronometer 計時器:%s");
49: }
50:
51: private void findViews(){
52: btnStart = (Button) findViewById(R.id.startBtn);
53: btnStop = (Button) findViewById(R.id.stopBtn);
54: btnReset = (Button) findViewById(R.id.resetBtn);
55: btnExit = (Button) findViewById(R.id.exitBtn);
56: chronometer = (Chronometer) findViewById(R.id.chronometer);
57: }
58:
59: private void setListeners() {
60: btnStart.setOnClickListener(this);
61: btnStop.setOnClickListener(this);
62: btnReset.setOnClickListener(this);
63: btnExit.setOnClickListener(this);
64: chronometer.setOnChronometerTickListener(this);
65: }
66:
67: }
<main.xml>
1: <?xml version="1.0" encoding="utf-8"?>
2: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3: android:layout_width="fill_parent"
4: android:layout_height="fill_parent"
5: android:orientation="vertical" >
6:
7: <Button
8: android:id="@+id/startBtn"
9: android:layout_width="match_parent"
10: android:layout_height="wrap_content"
11: android:text="@string/strStart" />
12:
13: <Button
14: android:id="@+id/stopBtn"
15: android:layout_width="match_parent"
16: android:layout_height="wrap_content"
17: android:text="@string/strStop" />
18:
19: <Button
20: android:id="@+id/resetBtn"
21: android:layout_width="match_parent"
22: android:layout_height="wrap_content"
23: android:text="@string/strReset" />
24:
25: <Button
26: android:id="@+id/exitBtn"
27: android:layout_width="match_parent"
28: android:layout_height="wrap_content"
29: android:text="@string/strExit" />
30:
31: <Chronometer
32: android:id="@+id/chronometer"
33: android:layout_width="wrap_content"
34: android:layout_height="wrap_content"
35: android:text="@string/strNull" />
36:
37: </LinearLayout>
<strings.xml>
1: <?xml version="1.0" encoding="utf-8"?>
2: <resources>
3:
4: <string name="hello">Hello World, ChronometerClock!</string>
5: <string name="app_name">ChronometerClock</string>
6: <string name="strStart">開始</string>
7: <string name="strStop">暫停</string>
8: <string name="strReset">歸零</string>
9: <string name="strExit">離開</string>
10: <string name="strNull"></string>
11:
12:
13: </resources>
沒有留言:
張貼留言