學(xué)習(xí)使用Android Chronometer計(jì)時(shí)器
本文實(shí)例為大家分享了Android Chronometer計(jì)時(shí)器基本使用方法,供大家參考,具體內(nèi)容如下
在默認(rèn)情況下,Chronometer組件只輸出MM:SS或H:MM:SS的時(shí)間格式。例如,當(dāng)計(jì)時(shí)到1分20秒時(shí),Chronometer組件會(huì)顯示01:20。如果想改變顯示的信息內(nèi)容,可以使用Chronometer類的setFormat方法。該方法需要一個(gè)String變量,并使用"%s"表示計(jì)時(shí)信息。例如,使用setFormat("計(jì)時(shí)信息:%s")設(shè)置顯示信息,Chronometer組件會(huì)顯示如下計(jì)時(shí)信息:
計(jì)時(shí)信息:10:20
android:format;//定義時(shí)間的格式如:hh:mm:ss setFormat("計(jì)時(shí):%s"); //設(shè)置顯示格式 setFormat(String format);//設(shè)置顯示時(shí)間的格式。 start();//開始計(jì)時(shí) stop();//停止計(jì)時(shí) setBase();//設(shè)置基地時(shí)間,一般都是SystemClock.elapsedRealtime() setOnChronometerTickListener(Chronometer.OnChronometerTickListener listener);//當(dāng)計(jì)時(shí)器改變時(shí)調(diào)用
案例:
1.定義布局文件chronometer.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Chronometer android:id="@+id/chronometer" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/chronometer_start" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="開始計(jì)時(shí)" /> <Button android:id="@+id/chronometer_end" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="停止計(jì)時(shí)" /> <Button android:id="@+id/chronometer_null" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="重新開始計(jì)時(shí)" /> </LinearLayout>
2.java代碼文件:ChronometerDemo.java
package com.test; 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 chronometer; private Button button_start, button_end,button_bull; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.chronometer); chronometer =(Chronometer)findViewById(R.id.chronometer); button_start =(Button)findViewById(R.id.chronometer_start); button_end =(Button)findViewById(R.id.chronometer_end); button_bull =(Button)findViewById(R.id.chronometer_null); button_start.setOnClickListener(clickListener); button_end.setOnClickListener(clickListener); button_bull.setOnClickListener(clickListener); } private OnClickListener clickListener = new OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.chronometer_start: //調(diào)用start()方法開始計(jì)時(shí) chronometer.start(); button_start.setText("正在計(jì)時(shí)..."); break; case R.id.chronometer_end: //調(diào)用stop()方法停止計(jì)時(shí) chronometer.stop(); button_start.setText("繼續(xù)計(jì)時(shí)"); break; case R.id.chronometer_null: //調(diào)用stop()方法停止計(jì)時(shí) chronometer.setBase(SystemClock.elapsedRealtime()); chronometer.start(); button_start.setText("正在計(jì)時(shí)..."); break; default: break; } } }; }
3. 運(yùn)行效果:
有一個(gè)問題就是,計(jì)時(shí)器開始計(jì)時(shí)后,一段時(shí)間之后,點(diǎn)擊停止計(jì)時(shí),一段時(shí)間后,點(diǎn)擊繼續(xù)計(jì)時(shí),但是這個(gè)時(shí)間的不是停止后的那個(gè)時(shí)間。說明停止之后,計(jì)時(shí)器還在后臺(tái)運(yùn)行。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)Android Chronometer計(jì)時(shí)器有所幫助和啟發(fā)。
- Android編程之計(jì)時(shí)器Chronometer簡(jiǎn)單示例
- Android計(jì)時(shí)器的三種實(shí)現(xiàn)方式(Chronometer、Timer、handler)
- Android Chronometer控件實(shí)現(xiàn)計(jì)時(shí)器函數(shù)詳解
- Android計(jì)時(shí)器chronometer使用實(shí)例講解
- android之計(jì)時(shí)器(Chronometer)的使用以及常用的方法
- Android時(shí)分秒計(jì)時(shí)器的兩種實(shí)現(xiàn)方法
- android計(jì)時(shí)器,時(shí)間計(jì)算器的實(shí)現(xiàn)方法
- android開發(fā)教程之間隔執(zhí)行程序(android計(jì)時(shí)器)
- Android 編程下的計(jì)時(shí)器代碼
- Android計(jì)時(shí)器控件Chronometer應(yīng)用實(shí)例
相關(guān)文章
android當(dāng)前apn的狀態(tài)以及獲取方法
在絕大多數(shù)android機(jī)器etc路徑下存放一個(gè)的apns-conf.xml文件,表示當(dāng)前機(jī)器使用的apn信息通過root機(jī)器可以push出來看看,具體路徑可以上網(wǎng)搜下,接下來介紹獲取apn的狀態(tài)的方法2013-01-01Android 使用selector改變按鈕狀態(tài)實(shí)例詳解
這篇文章主要介紹了Android 使用selector改變按鈕狀態(tài)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-01-01Android中的webview支持頁面中的文件上傳實(shí)例代碼
本篇文章主要介紹了Android中的webview支持頁面中的文件上傳,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03android 仿微信聊天氣泡效果實(shí)現(xiàn)思路
微信聊天窗口的信息效果類似iphone上的短信效果,以氣泡的形式展現(xiàn),實(shí)現(xiàn)這種效果主要用到ListView和BaseAdapter,配合布局以及相關(guān)素材,接下來為大家介紹下如何實(shí)現(xiàn)2013-04-04Android 中TextView的使用imageview被壓縮問題解決辦法
這篇文章主要介紹了Android 中TextView的使用imageview被壓縮問題解決辦法的相關(guān)資料,需要的朋友可以參考下2017-04-04Android基礎(chǔ)教程數(shù)據(jù)存儲(chǔ)之文件存儲(chǔ)
這篇文章主要介紹了Android基礎(chǔ)教程數(shù)據(jù)存儲(chǔ)之文件存儲(chǔ)的相關(guān)資料,數(shù)據(jù)存儲(chǔ)是Android開發(fā)的重要的知識(shí),這里提供了實(shí)例,需要的朋友可以參考下2017-07-07簡(jiǎn)單掌握Android Widget桌面小部件的創(chuàng)建步驟
這篇文章主要介紹了簡(jiǎn)單掌握Android Widget桌面小部件的創(chuàng)建步驟,Widget一般采用web前端技術(shù)進(jìn)行開發(fā),需要的朋友可以參考下2016-03-03Android Toast實(shí)現(xiàn)全屏顯示
這篇文章主要為大家詳細(xì)介紹了Android Toast實(shí)現(xiàn)全屏顯示,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08