Android自帶倒計(jì)時(shí)控件Chronometer使用方法詳解
更新時(shí)間:2016年11月10日 11:28:52 作者:Android_小新哥哥
這篇文章主要為大家詳細(xì)介紹了Android自帶倒計(jì)時(shí)控件Chronometer的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
公司的以前的項(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" />
方法簡介:

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());
}
以上就是本文的全部內(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)APP可切換多語言步驟詳解
如果是單獨(dú)給app加上國際化,其實(shí)很容易,創(chuàng)建對(duì)應(yīng)的國家資源文件夾即可,如values-en,values-pt,這篇文章主要介紹了Android?實(shí)現(xiàn)APP可切換多語言,需要的朋友可以參考下2023-11-11
Android ProgressDialog的實(shí)例詳解
這篇文章主要介紹了Android ProgressDialog的實(shí)例詳解的相關(guān)資料,Android 開發(fā)項(xiàng)目的時(shí)候經(jīng)常會(huì)遇到耗時(shí)的操作,這里就講下Android ProgressDialog的應(yīng)用,需要的朋友可以參考下2017-07-07
Android Studio升級(jí)到4.1以后插件問題解決
這篇文章主要介紹了Android Studio升級(jí)到4.1以后插件問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10

