Android計(jì)時(shí)器chronometer使用實(shí)例講解
在Android中,可以使用計(jì)時(shí)器來(lái)實(shí)現(xiàn)對(duì)時(shí)間的監(jiān)測(cè),這個(gè)類所實(shí)現(xiàn)的功能有開始計(jì)時(shí),停止計(jì)時(shí),重新計(jì)時(shí),設(shè)置計(jì)
時(shí)模式,下面列出計(jì)時(shí)器方法的原型:
long getBase();//返回基地的時(shí)間,由setBase(long)設(shè)置的
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格式的
下面就來(lái)介紹一個(gè)實(shí)例:
package com.example.android.apis.view; // Need the following import to get access to the app resources, since this // class is in a sub-package. import com.example.android.apis.R; import android.app.Activity; import android.os.Bundle; import android.os.SystemClock; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Chronometer; public class ChronometerDemo extends Activity { Chronometer mChronometer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.chronometer); Button button; mChronometer = (Chronometer) findViewById(R.id.chronometer); // Watch for button clicks. button = (Button) findViewById(R.id.start); button.setOnClickListener(mStartListener);//對(duì)應(yīng)的是開始計(jì)時(shí) button = (Button) findViewById(R.id.stop); button.setOnClickListener(mStopListener); //對(duì)應(yīng)的是停止計(jì)時(shí) button = (Button) findViewById(R.id.reset); button.setOnClickListener(mResetListener);//對(duì)應(yīng)的是重新置數(shù) button = (Button) findViewById(R.id.set_format); button.setOnClickListener(mSetFormatListener);//對(duì)應(yīng)的是設(shè)置時(shí)間的顯示格式 button = (Button) findViewById(R.id.clear_format); button.setOnClickListener(mClearFormatListener);//對(duì)應(yīng)的是使用非格式的計(jì)時(shí)顯示功能 } View.OnClickListener mStartListener = new OnClickListener() { public void onClick(View v) { mChronometer.start(); } }; View.OnClickListener mStopListener = new OnClickListener() { public void onClick(View v) { mChronometer.stop(); } }; View.OnClickListener mResetListener = new OnClickListener() { public void onClick(View v) { mChronometer.setBase(SystemClock.elapsedRealtime()); } }; View.OnClickListener mSetFormatListener = new OnClickListener() { public void onClick(View v) { mChronometer.setFormat("Formatted time (%s)"); } }; View.OnClickListener mClearFormatListener = new OnClickListener() { public void onClick(View v) { mChronometer.setFormat(null); } }; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)使用Android計(jì)時(shí)器有所幫助,謝謝大家的閱讀。
- android之計(jì)時(shí)器(Chronometer)的使用以及常用的方法
- Android時(shí)分秒計(jì)時(shí)器的兩種實(shí)現(xiàn)方法
- Android 編程下的計(jì)時(shí)器代碼
- Android實(shí)現(xiàn)的秒表計(jì)時(shí)器示例
- Android計(jì)時(shí)器的三種實(shí)現(xiàn)方式(Chronometer、Timer、handler)
- Android編程之簡(jiǎn)單計(jì)時(shí)器實(shí)現(xiàn)方法
- Android中CountDownTimer倒計(jì)時(shí)器用法實(shí)例
- Android Chronometer控件實(shí)現(xiàn)計(jì)時(shí)器函數(shù)詳解
- Android開發(fā)實(shí)現(xiàn)的計(jì)時(shí)器功能示例
相關(guān)文章
ERROR/AndroidRuntime(17121)的問題解決
ERROR/AndroidRuntime(17121)的問題解決,需要的朋友可以參考一下2013-05-05Android自定義View實(shí)現(xiàn)字母導(dǎo)航欄的代碼
這篇文章主要介紹了Android自定義View實(shí)現(xiàn)字母導(dǎo)航欄的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09Android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10Android Studio實(shí)現(xiàn)仿微信APP門戶界面詳解及源碼
這篇文章帶你通過Android studio來(lái)實(shí)現(xiàn)微信APP的門戶界面,主要說明框架的各部分功能與實(shí)現(xiàn)過程,下文包含了整個(gè)開發(fā)過程,以及解決問題的思路并再末尾提供了源碼鏈接2021-10-10Android微信右滑退出功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android微信右滑退出功能的實(shí)現(xiàn)代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-01-01Android編寫文件瀏覽器簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android編寫文件瀏覽器簡(jiǎn)單實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11

Android studio實(shí)現(xiàn)簡(jiǎn)單計(jì)算器