Android控件Chronometer定時(shí)器的實(shí)現(xiàn)方法
Chronometer是一個(gè)簡單的定時(shí)器,你可以給它一個(gè)開始時(shí)間,并以此定時(shí),或者如果你不給它一個(gè)開始時(shí)間,它將會(huì)使用你的時(shí)間通話開始。默認(rèn)情況下它會(huì)顯示在當(dāng)前定時(shí)器的值的形式“分:秒”或“H:MM:SS的”,或者可以使用的Set(字符串)格式的定時(shí)器值到一個(gè)任意字符串
1.重要屬性
android:format:定義時(shí)間的格式如:hh:mm:ss
2.重要方法
setBase(long base):設(shè)置倒計(jì)時(shí)定時(shí)器
setFormat(String format):設(shè)置顯示時(shí)間的格式。
start():開始計(jì)時(shí)
stop() :停止計(jì)時(shí)
setOnChronometerTickListener(Chronometer.OnChronometerTickListener listener):當(dāng)計(jì)時(shí)器改變時(shí)調(diào)用。
3.實(shí)例
布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip" android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <Chronometer android:id="@+id/chronometer" android:format="Initial format: " android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0" android:paddingBottom="30dip" android:paddingTop="30dip" /> <Button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="開始"> <requestFocus /> </Button> <Button android:id="@+id/stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止"> </Button> <Button android:id="@+id/reset" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="重置"> </Button> <Button android:id="@+id/set_format" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="設(shè)置格式"> </Button> <Button android:id="@+id/clear_format" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清除格式"> </Button> </LinearLayout>
主程序:
package wjq.WidgetDemo; 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 { private Chronometer mChronometer; /* (non-Javadoc) * @see android.app.Activity#onCreate(android.os.Bundle) */ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.chronometerpage); Button button; mChronometer = (Chronometer) findViewById(R.id.chronometer); // Watch for button clicks. button = (Button) findViewById(R.id.start); button.setOnClickListener(mStartListener); button = (Button) findViewById(R.id.stop); button.setOnClickListener(mStopListener); button = (Button) findViewById(R.id.reset); button.setOnClickListener(mResetListener); button = (Button) findViewById(R.id.set_format); button.setOnClickListener(mSetFormatListener); button = (Button) findViewById(R.id.clear_format); button.setOnClickListener(mClearFormatListener); } 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); } }; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android定時(shí)器Timer的停止和重啟實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)定時(shí)器的五種方法實(shí)例詳解
- Android 實(shí)現(xiàn)定時(shí)器的四種方式總結(jié)及實(shí)現(xiàn)實(shí)例
- Android 三種實(shí)現(xiàn)定時(shí)器詳解及實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)定時(shí)器的3種方法
- Android中使用定時(shí)器的三種方法
- Android三種實(shí)現(xiàn)定時(shí)器的方法
- 基于Android中實(shí)現(xiàn)定時(shí)器的3種解決方法
- 詳解Android實(shí)現(xiàn)定時(shí)器的幾種方法
相關(guān)文章
Android EventBus 3.0.0 使用總結(jié)(必看篇)
下面小編就為大家?guī)硪黄狝ndroid EventBus 3.0.0 使用總結(jié)(必看篇)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05android 類似微信的搖一搖功能實(shí)現(xiàn)思路及代碼
微信的搖一搖功能的出現(xiàn),讓彼此之間的距離有近了一步,本文也想實(shí)現(xiàn)以下微信的搖一搖功能,感興趣的朋友可以了解下啊,希望本人對你有所幫助2013-01-01Android源碼學(xué)習(xí)之觀察者模式應(yīng)用及優(yōu)點(diǎn)介紹
定義對象間一種一對多的依賴關(guān)系,使得當(dāng)一個(gè)對象改變狀態(tài),則所有依賴于它的對象都會(huì)得到通知并被自動(dòng)更新等等,需要了解的朋友可以參考下2013-01-01實(shí)現(xiàn)activity管理器一次退出所有activity
退出所有Activity網(wǎng)上有很多很多種說法,推薦的一種方法是自定義一個(gè)Activity管理器,來管理所有已打開的Activity,要退出的時(shí)候再通過這個(gè)管理器來退出所有Activity,下面是一個(gè)簡單的Activity管理器代碼2014-01-01Android SwipeMenuListView框架詳解分析
這篇文章主要介紹了Android SwipeMenuListView框架詳解分析的相關(guān)資料,需要的朋友可以參考下2016-10-10Android自定義超級(jí)炫酷的ViewPage指示器
由于應(yīng)公司開發(fā)要求,有一個(gè)顏色漸變帶縮放的指示器,雖然網(wǎng)上很多大佬開源的指示器開源庫,但如果一直都是使用別人造的輪子,那么對于自身的能力是毫無提升作用的,即使是參考別人的,然后自己動(dòng)手寫一遍那對于自身來說也是一種升華2022-07-07Android Kotlin環(huán)境使用ButterKnife的方法
本篇文章主要介紹了Android Kotlin環(huán)境使用ButterKnife的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03Android實(shí)現(xiàn)開機(jī)自動(dòng)啟動(dòng)Service或app的方法
這篇文章主要介紹了Android實(shí)現(xiàn)開機(jī)自動(dòng)啟動(dòng)Service或app的方法,結(jié)合實(shí)例形式分析了Android開機(jī)自啟動(dòng)程序的具體步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-07-07