Android時(shí)分秒計(jì)時(shí)器的兩種實(shí)現(xiàn)方法
可能我們在開發(fā)中會時(shí)常用到計(jì)時(shí)器這玩意兒,比如在錄像的時(shí)候,我們可能需要在右上角顯示一個(gè)計(jì)時(shí)器。這個(gè)東西其實(shí)實(shí)現(xiàn)起來非常簡單。
只需要用一個(gè)控件Chronometer,是的,就這么簡單,我都不好意思講述一下了。
<Chronometer android:layout_width="wrap_content" android:layout_height="wrap_content" android:format="%s" android:id="@+id/timer"/>
是的,就這么簡單。java代碼同樣
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); timer = (Chronometer) findViewById(R.id.timer); } public void btnClick(View view) { timer.setBase(SystemClock.elapsedRealtime());//計(jì)時(shí)器清零 timer.start(); }
超簡單有木有?看看運(yùn)行結(jié)果:
或許你會說,這個(gè)要是需要顯示上時(shí)間怎么弄呢?不急不急,兩行代碼就能解決的事情。
public void btnClick(View view) { timer.setBase(SystemClock.elapsedRealtime());//計(jì)時(shí)器清零 int hour = (int) ((SystemClock.elapsedRealtime() - timer.getBase()) / 1000 / 60); timer.setFormat("0"+String.valueOf(hour)+":%s"); timer.start(); } public void stopClick(View view) { timer.stop(); }
恩,對,就是 這么簡單,不過別忘了把xml的format改一下
<Chronometer android:layout_width="match_parent" android:layout_height="wrap_content" android:format="00:00:00" android:gravity="center" android:id="@+id/timer"/>
是的,你沒有看錯,這樣就可以了,不信,你看!
就和你想象的錄像上方的時(shí)間一樣有木有?恩。你前面設(shè)置一個(gè)圓圈,再設(shè)置計(jì)時(shí)器顏色就和它一樣有逼格了。
而或許你并不喜歡用這種方式,當(dāng)然用handler+timer+timerTask的方式也是可以的啦。由于太簡單,就直接上代碼了。
package com.example.nanchen.timerdemo; import android.os.SystemClock; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Chronometer; import android.widget.TextView; import java.util.Locale; import java.util.Timer; import java.util.TimerTask; public class MainActivity extends AppCompatActivity { private Chronometer timer; private Timer timer1; private TextView textView; private TimerTask timerTask; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); timer = (Chronometer) findViewById(R.id.timer); textView = (TextView) findViewById(R.id.text); timer1 = new Timer(); } public void btnClick(View view) { timer.setBase(SystemClock.elapsedRealtime());//計(jì)時(shí)器清零 int hour = (int) ((SystemClock.elapsedRealtime() - timer.getBase()) / 1000 / 60); timer.setFormat("0"+String.valueOf(hour)+":%s"); timer.start(); } public void stopClick(View view) { timer.stop(); } public void startClick(View view) { timerTask = new TimerTask() { int cnt = 0; @Override public void run() { runOnUiThread(new Runnable() { @Override public void run() { textView.setText(getStringTime(cnt++)); } }); } }; timer1.schedule(timerTask,0,1000); } private String getStringTime(int cnt) { int hour = cnt/3600; int min = cnt % 3600 / 60; int second = cnt % 60; return String.format(Locale.CHINA,"%02d:%02d:%02d",hour,min,second); } public void stopClick1(View view) { if (!timerTask.cancel()){ timerTask.cancel(); timer1.cancel(); } } }
<?xml version="1.0" encoding="utf-8"?> <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" tools:context="com.example.nanchen.timerdemo.MainActivity"> <Chronometer android:layout_width="match_parent" android:layout_height="wrap_content" android:format="00:00:00" android:gravity="center" android:id="@+id/timer"/> <Button android:layout_width="match_parent" android:onClick="btnClick" android:text="start" android:layout_height="wrap_content"/> <Button android:layout_width="match_parent" android:text="stop" android:onClick="stopClick" android:layout_height="wrap_content"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#959393" android:layout_marginBottom="20dp" android:layout_marginTop="20dp"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="00:00:00" android:gravity="center" android:id="@+id/text"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="開始" android:onClick="startClick"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="停止" android:onClick="stopClick1"/> </LinearLayout>
簡單運(yùn)行下方用timer實(shí)現(xiàn)的效果:
想必大家到這樣都會有了自己的理解,android 官方的Chronometer方式只是為了做一個(gè)計(jì)時(shí)器,而我們采用自己用Timer和TimerTask方式可以更加自主,因?yàn)槟憧梢韵霃氖裁磿r(shí)間開始計(jì)時(shí)就從什么時(shí)間開始計(jì)時(shí),計(jì)時(shí)方式想順計(jì)時(shí)倒計(jì)時(shí)都不是難事兒,甚至各種浮夸的隔兩秒,隔三秒,隔n秒都是可以的,具體使用就看你選擇咯~~
以上所述是小編給大家介紹的Android時(shí)分秒計(jì)時(shí)器的兩種實(shí)現(xiàn)方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- android之計(jì)時(shí)器(Chronometer)的使用以及常用的方法
- android計(jì)時(shí)器,時(shí)間計(jì)算器的實(shí)現(xiàn)方法
- android開發(fā)教程之間隔執(zhí)行程序(android計(jì)時(shí)器)
- Android實(shí)現(xiàn)的秒表計(jì)時(shí)器示例
- Android計(jì)時(shí)器的三種實(shí)現(xiàn)方式(Chronometer、Timer、handler)
- Android 編程下的計(jì)時(shí)器代碼
- Android編程之簡單計(jì)時(shí)器實(shí)現(xiàn)方法
- Android開發(fā)實(shí)現(xiàn)的計(jì)時(shí)器功能示例
- Android中CountDownTimer倒計(jì)時(shí)器用法實(shí)例
- Android實(shí)現(xiàn)計(jì)時(shí)器功能
相關(guān)文章
Android添加指紋解鎖功能的實(shí)現(xiàn)代碼
當(dāng)開發(fā)的APP需要加密驗(yàn)證時(shí)可以考慮添加系統(tǒng)指紋解鎖功能。這篇文章主要介紹了Android添加指紋解鎖功能的實(shí)現(xiàn)代碼,需要的朋友可以參考下2018-07-07Android模擬器安裝APP出現(xiàn)INSTALL_FAILED_NO_MATCHING_ABIS錯誤解決方案
這篇文章主要介紹了 Android模擬器安裝APP出現(xiàn)INSTALL_FAILED_NO_MATCHING_ABIS錯誤解決方案的相關(guān)資料,需要的朋友可以參考下2016-12-12Android開發(fā)之TabActivity用法實(shí)例詳解
這篇文章主要介紹了Android開發(fā)之TabActivity用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android擴(kuò)展Activity實(shí)現(xiàn)標(biāo)簽頁效果的具體步驟與相關(guān)技巧,需要的朋友可以參考下2016-03-03