秒表 分:秒:毫秒

1、简介

秒表计时
分:秒:毫秒
在这里插入图片描述

2、文件结构

在这里插入图片描述

3、activity_main.xml 布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="00:00:00"
        android:textSize="50dp"
        />

    <Button
        android:id="@+id/but_start_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始"
        android:textSize="50dp"
        android:layout_marginTop="20dp"/>


    <Button
        android:id="@+id/but_stop_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="结束"
        android:textSize="50dp"
        android:layout_marginTop="20dp"/>

</LinearLayout>

4、mainActivity 功能文件

package com.example.tssh.mystopwatch;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.sql.Time;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private String TAG = "MainActivity: ";

    private TextView textView;
    private Button buttonStart,buttonStop;

    private long mlCount = 0; //计时 次数
    private int  mSecRate = 10; // 10 ms 刷新一次
    private String timeShow = "";
    private Timer mTimer1;
    private TimerTask mTask1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.text_id);
        buttonStart = (Button) findViewById(R.id.but_start_id);
        buttonStop = (Button) findViewById(R.id.but_stop_id);

        buttonStart.setOnClickListener(this);
        buttonStop.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.but_start_id:
                mlCount = 0;
                startWatch();
                break;
            case R.id.but_stop_id:
                stopTimeShow();
                break;
                default:
                    break;
        }
    }

    //开始计时
    private void startWatch() {
        if (mTimer1 == null && mTask1 == null) {
            mTimer1 = new Timer();
            mTask1 = new TimerTask() {
                @Override
                public void run() {
                    Message message = mHandler.obtainMessage(1);
                    mHandler.sendMessage(message);
                }
            };
            mTimer1.schedule(mTask1, 0, mSecRate);  //10 ms 刷新一次
        }

    }


    /**
     * 计时器
     */
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            switch (msg.what) {
                case 1:
                    mlCount++;
                    judgeTimeShow(mlCount);
                    break;
                default:
                    break;
            }
            super.handleMessage(msg);
        }

    };

    //停止刷新显示
    private void stopTimeShow() {
        if (mTimer1 != null) {
            mTimer1.cancel();
            mTimer1 = null;
        }
        if (mTask1 != null) {
            mTask1.cancel();
            mTask1 = null;
        }
    }

    //判断显示
    private String  judgeTimeShow(long mlCount) {
        String str ="";
        long min = 0;
        long sec = 0;
        long mSec = 0;
        if (mlCount <=0) {
            str = "00:00:00";
        } else {
            sec = mlCount / (1000/mSecRate);  // 由毫秒 计算出 秒
            if (sec < 60) {
                mSec = mlCount % (1000/mSecRate);  //剩余下的 毫秒
            } else {
                min = sec / 60;    //由秒计算出 min
                if (min > 99) {
                    str = "99:59:59";
                    textView.setText(str);
                    return str;
                }
                sec = sec % 60;
                mSec = mlCount - (min * 60 * (1000/mSecRate)) - (sec * (1000/mSecRate));
            }

            str = judgeSingleNum(min) + ":"+ judgeSingleNum(sec) + ":"+ judgeSingleNum(mSec);
        }

        Log.i(TAG,"时间是 mlCount:" + mlCount + "\n"
        + "设置的时间是:" + str);
        textView.setText(str);
        return str;
    }

    //判断是不是要加上0
    private String judgeSingleNum(long mlCount) {
        String strData = "";
        if (mlCount < 10) {
            strData = "0" + mlCount;
        } else {
                strData = mlCount + "";
        }
        return strData;
    }

}

5、log 显示

在这里插入图片描述

文献参考:
计时器–精确到10毫秒(精确度可以自行设定)
https://blog.csdn.net/ada498607067/article/details/38364863

Android用5种方式实现自定义计时器, 哪种才是你的菜?
https://blog.csdn.net/s793223706/article/details/81559654

猜你喜欢

转载自blog.csdn.net/qq_27061049/article/details/85093591