MainActivity
package com.example.clock;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Date;
public class MainActivityextends AppCompatActivityimplements Button.OnClickListener{
Buttonstart, stop, clear;
? ? TextViewshow;
? ? Datebgtime;
? ? long bg =0;
? ? boolean isFirst=true, isStop=false;
? ? Handlerhandler =new Handler();
? ? private Runnablerunnable =new Runnable() {
@Override
? ? ? ? public void run() {
if(!isStop){
show.setText(timeFormat(bg + (new Date().getTime()) -bgtime.getTime()));
? ? ? ? ? ? ? ? handler.post(this);
? ? ? ? ? ? }
}
};
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? start = (Button) findViewById(R.id.start);
? ? ? ? clear = (Button) findViewById(R.id.clear);
? ? ? ? stop = (Button) findViewById(R.id.stop);
? ? ? ? start.setOnClickListener(this);
? ? ? ? stop.setOnClickListener(this);
? ? ? ? clear.setOnClickListener(this);
? ? ? ? show = (TextView) findViewById(R.id.show);
? ? }
@Override
? ? public void onClick(View view) {
switch (view.getId()){
case R.id.start:
if(isFirst){
isFirst =false;
? ? ? ? ? ? ? ? ? ? isStop =false;
? ? ? ? ? ? ? ? ? ? bgtime =new Date();
? ? ? ? ? ? ? ? ? ? new Thread(runnable).run();
? ? ? ? ? ? ? ? }
break;
? ? ? ? ? ? case R.id.stop:
bg += (new Date().getTime()) -bgtime.getTime();
? ? ? ? ? ? ? ? isStop =true;
? ? ? ? ? ? ? ? isFirst =true;
break;
? ? ? ? ? ? case R.id.clear:
show.setText("00:00:00.00");
? ? ? ? ? ? ? ? bg =0;
? ? ? ? ? ? ? ? isStop =true;
? ? ? ? ? ? ? ? isFirst =true;
break;
? ? ? ? }
}
public StringtimeFormat(long ms)
{
int sm = (int) (ms%1000)/10;
? ? ? ? ms = (int) ms/1000;
? ? ? ? int sec = (int) ms%60;
? ? ? ? int min = (int) ms/60;
? ? ? ? int h = (int) ms/3600;
? ? ? ? return String.format("%02d:%02d:%02d.%02d",h, min, sec, sm);
? ? }
}
layout.xml
? ? 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:rowCount="4"
? ? android:columnCount="3"
? ? tools:context="com.example.clock.MainActivity"
? ? >
? ? ? ? android:id="@+id/textView"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_column="1"
? ? ? ? android:layout_gravity="center_horizontal"
? ? ? ? android:layout_row="0"
? ? ? ? android:text="計(jì)時(shí)器"
? ? ? ? android:textSize="30dp"/>
? ? ? ? android:id="@+id/show"
? ? ? ? android:layout_columnSpan="3"
? ? ? ? android:text="00:00:00.00"
? ? ? ? android:textSize="40dp"
? ? ? ? android:layout_gravity="center_horizontal"/>
? ? ? ? android:id="@+id/clear"
? ? ? ? android:text="清零"
? ? ? ? android:layout_row="2"
? ? ? ? android:layout_column="0"/>
? ? ? ? android:id="@+id/start"
? ? ? ? android:text="計(jì)時(shí)"
? ? ? ? android:layout_row="2"
? ? ? ? android:layout_column="1"
? ? ? ? android:layout_gravity="center_horizontal"/>
? ? ? ? android:id="@+id/stop"
? ? ? ? android:text="停止"
? ? ? ? android:layout_row="2"
? ? ? ? android:layout_column="2"/>