3B46D0D2C9BAC8DE182A4E559D0DCF090B2A18D03AB7D1F50F0F94E9D052  

<StopWatch.java>
1: package com.android.stopwatch;
2:
3: import java.text.DecimalFormat;
4:
5: import android.app.Activity;
6: import android.graphics.Color;
7: import android.os.Bundle;
8: import android.os.Handler;
9: import android.view.View;
10: import android.view.View.OnClickListener;
11: import android.widget.Button;
12: import android.widget.TextView;
13:
14: public class StopWatch extends Activity {
15:
16: private Button btnStartPause, btnExit;
17: private TextView tvTimerShow;
18: private Long intCnt = (long) 0;
19: private Handler handler = new Handler();
20: private Boolean flag = true;
21:
22: /** Called when the activity is first created. */
23: @Override
24: public void onCreate(Bundle savedInstanceState) {
25: super.onCreate(savedInstanceState);
26: setContentView(R.layout.main);
27: findViews();
28: setListeners();
29: }
30:
31:
32: private void findViews() {
33: tvTimerShow = (TextView) findViewById(R.id.timerTextView);
34: btnStartPause = (Button) findViewById(R.id.startpauseBtn);
35: btnExit = (Button) findViewById(R.id.exitBtn);
36: btnStartPause.setText(R.string.strStart);
37: btnStartPause.setBackgroundColor(Color.GREEN);
38: }
39:
40:
41: private void setListeners(){
42:
43: btnStartPause.setOnClickListener(new OnClickListener(){
44: public void onClick(View v) {
45: if ( flag ) {
46: flag = !flag;
47: btnStartPause.setText(R.string.strPause);
48: btnStartPause.setBackgroundColor(Color.RED);
49: handler.removeCallbacks(updateTimer);
50: handler.postDelayed(updateTimer,1000);
51: }
52: else {
53: flag = !flag;
54: btnStartPause.setText(R.string.strContinue);
55: btnStartPause.setBackgroundColor(Color.GREEN);
56: handler.removeCallbacks(updateTimer);
57: }
58:
59: }
60: });
61:
62:
63: btnExit.setOnClickListener(new OnClickListener(){




64: public void onClick(View v) {
65: android.os.Process.killProcess(android.os.Process.myPid());
66: }
67: });
68: }
69:
70: private Runnable updateTimer = new Runnable() {
71: public void run() {
72: DecimalFormat nf = new DecimalFormat("00");
73: intCnt = intCnt + 1;
74: Long minius = intCnt / 60;
75: Long seconds = intCnt % 60;
76: tvTimerShow.setText(nf.format(minius)+":"+nf.format(seconds));
77: handler.postDelayed(updateTimer,1000);
78: }
79: };
80: }

 

<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/startpauseBtn"
9: android:layout_width="match_parent"
10: android:layout_height="wrap_content"
11: android:text="@string/strStart" />
12:
13: <Button
14: android:id="@+id/exitBtn"
15: android:layout_width="match_parent"
16: android:layout_height="wrap_content"
17: android:text="@string/strExit" />
18:
19: <RelativeLayout
20: android:id="@+id/relativeLayout1"
21: android:layout_width="match_parent"
22: android:layout_height="wrap_content" >
23:
24:
25: <TextView
26: android:id="@+id/timerTextView"
27: android:layout_width="wrap_content"
28: android:layout_height="wrap_content"
29: android:layout_alignParentTop="true"
30: android:layout_centerHorizontal="true"
31: android:text="@string/strNull"
32: android:textSize="50sp" />
33:
34: </RelativeLayout>
35:
36: </LinearLayout>

 

<strings.xml> 1: <?xml version= "1.0" encoding= "utf-8" ?>

 

2: <resources>
3:
4: <string name="hello">Hello World, StopWatch!</string>
5: <string name="app_name">StopWatch</string>
6: <string name="strStart">開始</string>
7: <string name="strExit">離開</string>
8: <string name="strPause">暫停</string>
9: <string name="strNull"></string>
10: <string name="strContinue">繼續</string>
11:
12: </resources>





創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(1) 人氣()