java:
private Handler handler = new Handler();
handler.removeCallbacks(updateTimer); //銷毀執行緒
//取得目前時間
startTime = System.currentTimeMillis();
//設定Delay的時間
handler.postDelayed(updateTimer, 1000);
//固定要執行的方法
private Runnable updateTimer = new Runnable() {
public void run() {
final TextView time = (TextView) findViewById(R.id.textView);
Long spentTime = System.currentTimeMillis() - startTime;
//計算目前已過分鐘數
Long minius = (spentTime/1000)/60;
//計算目前已過秒數
Long seconds = (spentTime/1000) % 60;
handler.postDelayed(this, 1000);
time.setText("時間: "+minius+" 分鐘 "+seconds + " 秒鐘");
}
};
留言列表