2012年10月28日,手機沒有秒表,自己想做一個秒表來給自己用,現在馬上做出一個實例來,這只是開始,以后做個界面漂亮的應用出來。廢話不說,先上圖:

源碼:
建立項目:Stopwatch
代碼清單:org/wwj/Stopwatch/Stopwatch.java
package org.wwj.Stopwatch;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Stopwatch extends Activity {
private TextView minText; //分
private TextView secText; //秒
private Button start; //開始按鈕
private Button stop; //停止按鈕
private boolean isPaused = false;
private String timeUsed;
private int timeUsedInsec;
private Handler uiHandle = new Handler(){
public void handleMessage(android.os.Message msg) {
switch(msg.what){
case 1:
if(!isPaused) {
addTimeUsed();
updateClockUI();
}
uiHandle.sendEmptyMessageDelayed(1, 1000);
break;
default: break;
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stopwatch);
//獲取界面的控件
minText = (TextView) findViewById(R.id.min);
secText = (TextView) findViewById(R.id.sec);
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
//為按鈕Start注冊監聽器
start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
uiHandle.removeMessages(1);
startTime();
isPaused = false;
}
});
//為按鈕stop注冊監聽器
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
isPaused = true;
timeUsedInsec = 0;
}
});
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
isPaused = true;
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
isPaused = false;
}
private void startTime(){
uiHandle.sendEmptyMessageDelayed(1, 1000);
}
/**
* 更新時間的顯示
*/
private void updateClockUI(){
minText.setText(getMin() + ":");
secText.setText(getSec());
}
public void addTimeUsed(){
timeUsedInsec = timeUsedInsec + 1;
timeUsed = this.getMin() + ":" + this.getSec();
}
public CharSequence getMin(){
return String.valueOf(timeUsedInsec / 60);
}
public CharSequence getSec(){
int sec = timeUsedInsec % 60;
return sec < 10? "0" + sec :String.valueOf(sec);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_stopwatch, menu);
return true;
}
}
界面布局:res/layout/layout_stopwatch.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/min"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"/>
<TextView
android:id="@+id/sec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:textColor="#ff0000"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
>
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/start"/>
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/stop"/>
</LinearLayout>
</LinearLayout>
