以Timer元件做一個計時器來了解Timer的設計與應用
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="碼表:" />
<TextView
android:id="@+id/timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="開始" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止" />
<Button
android:id="@+id/zero"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="歸零" />
<Button
android:id="@+id/end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="結束" />
</LinearLayout>
</LinearLayout>
MainActivity.java:
package com.example.timer;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView timer;
private Button start;
private Button stop;
private Button zero;
private Button end;
private boolean startflag=false;
private int tsec=0,csec=0,cmin=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timer = (TextView)findViewById(R.id.timer);
start = (Button)findViewById(R.id.start);
stop = (Button)findViewById(R.id.stop);
zero = (Button)findViewById(R.id.zero);
end = (Button)findViewById(R.id.end);
//宣告Timer
Timer timer01 =new Timer();
//設定Timer(task為執行內容,0代表立刻開始,間格1秒執行一次)
timer01.schedule(task, 0,1000);
//Button監聽
start.setOnClickListener(listener);
stop.setOnClickListener(listener);
zero.setOnClickListener(listener);
end.setOnClickListener(listener);
}
//TimerTask無法直接改變元件因此要透過Handler來當橋樑
private Handler handler = new Handler(){
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch(msg.what){
case 1:
csec=tsec%60;
cmin=tsec/60;
String s="";
if(cmin <10 s="<span" style="color: #2a00ff;">"0"10>
+cmin;
}else{
s=""+cmin;
}
if(csec < 10){
s=s+":0"+csec;
}else{
s=s+":"+csec;
}
//s字串為00:00格式
timer.setText(s);
break;
}
}
};
private TimerTask task = new TimerTask(){
@Override
public void run() {
// TODO Auto-generated method stub
if (startflag){
//如果startflag==true則每秒tsec+1
tsec++;
Message message = new Message();
//傳送訊息1
message.what =1;
handler.sendMessage(message);
}
}
};
private OnClickListener listener =new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.start:
startflag=true;
break;
case R.id.stop:
startflag=false;
break;
case R.id.zero:
tsec=0;
//TextView 初始化
timer.setText("00:00");
break;
case R.id.end:
finish();
break;
}
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}