Android自帶倒計(jì)時(shí)控件Chronometer使用方法詳解
公司的以前的項(xiàng)目,看到使用了這個(gè)Android自帶的倒計(jì)時(shí)控件Chronometer,現(xiàn)在整合了一下
先看看效果:
<Chronometer android:id="@+id/chronometer" android:layout_width="wrap_content" android:layout_height="30dp" /> <Button android:onClick="start" android:text="開始" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:onClick="stop" android:text="停止" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:onClick="reset" android:text="重置" android:layout_width="wrap_content" android:layout_height="wrap_content" />
方法簡(jiǎn)介:
long getBase(); //返回基地的時(shí)間,由setBase(long)設(shè)置的,可以是SystemClock.elapsedRealtime() String getFormat();//返回當(dāng)前字符串格式,此格式是通過setFormat()實(shí)現(xiàn)的 void setBase(long base);//設(shè)置時(shí)間,計(jì)數(shù)定時(shí)器指定的值 void setFormat(String format);//設(shè)置顯示的內(nèi)容,計(jì)時(shí)器將會(huì)顯示這個(gè)參數(shù)所對(duì)應(yīng)的值得,如果字符串的值為null,那么返回的值為MM:SS格式的
private Chronometer chronometer; private long recordTime;//記錄下來的總時(shí)間 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); chronometer= (Chronometer) findViewById(R.id.chronometer); chronometer.setFormat("計(jì)時(shí):%s");//設(shè)置顯示的格式 Toast.makeText(MainActivity.this, ""+recordTime, Toast.LENGTH_SHORT).show(); } public void start(View view){ chronometer.setBase(SystemClock.elapsedRealtime()-recordTime);//跳過已經(jīng)記錄的時(shí)間 chronometer.start(); } public void stop(View view){ chronometer.stop(); recordTime=SystemClock.elapsedRealtime()-chronometer.getBase();//保存這次記錄的時(shí)間 } public void reset(View view){ recordTime=0;//重置時(shí)間 chronometer.setBase(SystemClock.elapsedRealtime()); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android自定義倒計(jì)時(shí)控件示例
- android實(shí)現(xiàn)倒計(jì)時(shí)功能代碼
- Android實(shí)現(xiàn)計(jì)時(shí)與倒計(jì)時(shí)的常用方法小結(jié)
- Android實(shí)現(xiàn)加載廣告圖片和倒計(jì)時(shí)的開屏布局
- Android 實(shí)現(xiàn)閃屏頁和右上角的倒計(jì)時(shí)跳轉(zhuǎn)實(shí)例代碼
- Android實(shí)現(xiàn)時(shí)間倒計(jì)時(shí)功能
- Android自定義圓形倒計(jì)時(shí)進(jìn)度條
- Android中使用TextView實(shí)現(xiàn)高仿京東淘寶各種倒計(jì)時(shí)效果
- Android 列表倒計(jì)時(shí)的實(shí)現(xiàn)的示例代碼(CountDownTimer)
- Android實(shí)現(xiàn)圓圈倒計(jì)時(shí)
相關(guān)文章
安卓(Android)實(shí)現(xiàn)3DTouch效果
3DTouch是什么效果的大家應(yīng)該都知道了。本文將介紹在Android中如何實(shí)現(xiàn)3DTouch的效果,有需要的可以參考學(xué)習(xí)。2016-08-08Android使用URL讀取網(wǎng)絡(luò)資源的方法
這篇文章主要為大家詳細(xì)介紹了Android使用URL讀取網(wǎng)絡(luò)資源的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Android快速開發(fā)系列 10個(gè)常用工具類實(shí)例代碼詳解
今天特此整理出10個(gè)基本每個(gè)項(xiàng)目中都會(huì)使用的工具類,用于快速開發(fā),對(duì)android開發(fā)常用工具類感興趣的朋友跟隨小編一起看看吧2018-09-09Android 兩個(gè)Fragment之間的跳轉(zhuǎn)和數(shù)據(jù)的傳遞實(shí)例詳解
這篇文章主要介紹了Android 兩個(gè)Fragment之間的跳轉(zhuǎn)和數(shù)據(jù)的傳遞實(shí)例詳解的相關(guān)資料,這里說明實(shí)現(xiàn)的思路及實(shí)現(xiàn)方法,需要的朋友可以參考下2017-07-07Android實(shí)現(xiàn)懸浮對(duì)話框代碼
這篇文章主要介紹了Android實(shí)現(xiàn)懸浮對(duì)話框代碼的相關(guān)資料,需要的朋友可以參考下2016-03-03Android Studio 修改應(yīng)用包名實(shí)例詳解
這篇文章主要介紹了Android Studio 修改應(yīng)用包名實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03Android開機(jī)自啟動(dòng)服務(wù)的實(shí)現(xiàn)方法
Android開機(jī)自啟動(dòng)服務(wù)的實(shí)現(xiàn)方法,需要的朋友可以參考一下2013-05-05Android Studio 2020新版本卡在Gradle downloading/sync failed/下載緩慢/
Android Studio 2020新版本 卡在Gradle downloading / sync failed / 下載緩慢 / 下載超時(shí) 親測(cè)有效解決辦法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-12-12Android Studio獲取網(wǎng)絡(luò)JSON數(shù)據(jù)并處理的方法
這篇文章主要為大家詳細(xì)介紹了Android Studio獲取網(wǎng)絡(luò)JSON數(shù)據(jù)并處理的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10