秒錶 分:秒:毫秒
阿新 • • 發佈:2018-12-20
文章目錄
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