Android自帶倒計時控件Chronometer使用方法詳解
公司的以前的項目,看到使用了這個Android自帶的倒計時控件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" />
方法簡介:
long getBase(); //返回基地的時間,由setBase(long)設(shè)置的,可以是SystemClock.elapsedRealtime() String getFormat();//返回當前字符串格式,此格式是通過setFormat()實現(xiàn)的 void setBase(long base);//設(shè)置時間,計數(shù)定時器指定的值 void setFormat(String format);//設(shè)置顯示的內(nèi)容,計時器將會顯示這個參數(shù)所對應(yīng)的值得,如果字符串的值為null,那么返回的值為MM:SS格式的
private Chronometer chronometer; private long recordTime;//記錄下來的總時間 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); chronometer= (Chronometer) findViewById(R.id.chronometer); chronometer.setFormat("計時:%s");//設(shè)置顯示的格式 Toast.makeText(MainActivity.this, ""+recordTime, Toast.LENGTH_SHORT).show(); } public void start(View view){ chronometer.setBase(SystemClock.elapsedRealtime()-recordTime);//跳過已經(jīng)記錄的時間 chronometer.start(); } public void stop(View view){ chronometer.stop(); recordTime=SystemClock.elapsedRealtime()-chronometer.getBase();//保存這次記錄的時間 } public void reset(View view){ recordTime=0;//重置時間 chronometer.setBase(SystemClock.elapsedRealtime()); }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android使用URL讀取網(wǎng)絡(luò)資源的方法
這篇文章主要為大家詳細介紹了Android使用URL讀取網(wǎng)絡(luò)資源的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10Android快速開發(fā)系列 10個常用工具類實例代碼詳解
今天特此整理出10個基本每個項目中都會使用的工具類,用于快速開發(fā),對android開發(fā)常用工具類感興趣的朋友跟隨小編一起看看吧2018-09-09Android 兩個Fragment之間的跳轉(zhuǎn)和數(shù)據(jù)的傳遞實例詳解
這篇文章主要介紹了Android 兩個Fragment之間的跳轉(zhuǎn)和數(shù)據(jù)的傳遞實例詳解的相關(guān)資料,這里說明實現(xiàn)的思路及實現(xiàn)方法,需要的朋友可以參考下2017-07-07Android Studio 修改應(yīng)用包名實例詳解
這篇文章主要介紹了Android Studio 修改應(yīng)用包名實例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03Android Studio 2020新版本卡在Gradle downloading/sync failed/下載緩慢/
Android Studio 2020新版本 卡在Gradle downloading / sync failed / 下載緩慢 / 下載超時 親測有效解決辦法,本文給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2020-12-12Android Studio獲取網(wǎng)絡(luò)JSON數(shù)據(jù)并處理的方法
這篇文章主要為大家詳細介紹了Android Studio獲取網(wǎng)絡(luò)JSON數(shù)據(jù)并處理的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10