?
本文檔使用 php中文網(wǎng)手冊 發(fā)布
Chronometer
農(nóng)民伯伯
版本:Android 2.2 r1
?
public class Chronometer extends TextView
?
java.lang.Object
android.view.View
? ???? android.widget.TextView
? ????????????? android.widget.Chronometer
?
概述
類實(shí)現(xiàn)了一個簡單的計時器。
你可以通過elapsedRealtime()
來
給它一個基準(zhǔn)時間,并從該時間開始計數(shù)。如果你不給它基準(zhǔn)時間,它將使用你調(diào)用start()
時的時間。默認(rèn)它將顯示當(dāng)前"MM:SS"或 "H:MM:SS"格式的時間,或者你能通過setFormat(String)
設(shè)置一個任意字符串來格式化顯示計時器顯示的時間。
?
XML屬性
屬性名稱 |
描述 |
android:format |
格式化字符串:如果指定,計時器將根據(jù)這個字符串來顯示,替換字符串中第一個“%s”為當(dāng)前"MM:SS"或 "H:MM:SS"格式的時間顯示。如果不指定,計時器將簡單的顯示"MM:SS"
or "H:MM:SS"格式的時間。(譯者注:如: |
?
構(gòu)造函數(shù)
???????? public Chronometer (Context context)
初始化計時器對象。設(shè)置當(dāng)前時間為基準(zhǔn)時間。(譯者注:通過程序動態(tài)創(chuàng)建計時器對象)
?
public Chronometer (Context context, AttributeSet attrs)
初始化標(biāo)準(zhǔn)視圖布局信息。設(shè)置當(dāng)前時間為基準(zhǔn)時間。(譯者注:指通過XML來指定一個計時器)
?
public Chronometer (Context context, AttributeSet attrs, int defStyle)
初始化標(biāo)準(zhǔn)視圖布局信息和風(fēng)格。設(shè)置當(dāng)前時間為基準(zhǔn)時間。
?
公共方法???????
???????? public long getBase ()
???????? 返回先前由setBase(long)設(shè)置的基準(zhǔn)時間。
?
???????? public String getFormat ()
???????? 返回先前由setFormat(String)設(shè)置的格式化字符串。
?
???????? public Chronometer.OnChronometerTickListener getOnChronometerTickListener ()
?????????????????? 返回值
??????????????????????????? 返回這個監(jiān)聽器(可能為空)是用于監(jiān)聽計時器變化的事件。
?
???????? public void setBase (long base)
???????? 設(shè)置基準(zhǔn)時間(譯者注:基準(zhǔn)時間為真正意義上開始計時的時間,而不是調(diào)用start時時間,比如調(diào)用本函數(shù)并設(shè)置參數(shù)base為SystemClock.elapsedRealtime()即表示從當(dāng)前時間開始重新計時)。
?????????????????? 參數(shù)
??????????????????????????? base ?????? 使用elapsedRealtime()為基準(zhǔn)時間
?
???????? public void setFormat (String format)
???????? 設(shè)置用于顯示的格式化字符串。格式化字符串:如果指定,計時器將根據(jù)這個字符串來顯示,替換字符串中第一個“%s”為當(dāng)前"MM:SS"或 "H:MM:SS"格式的時間顯示。如果這個格式化字符串為空,或者你從未調(diào)用過setFormat()方法,計時器將簡單的顯示"MM:SS" or
"H:MM:SS"格式的時間。(譯者注:如:"This is a Chronometer %s"
)
?????????????????? 參數(shù)
??????????????????????????? format ?? 格式化字符串
?
???????? public void setOnChronometerTickListener (Chronometer.OnChronometerTickListener listener)
???????? 設(shè)置計時器變化時調(diào)用的監(jiān)聽事件。
?????????????????? 參數(shù)
??????????????????????????? listener ? The listener.
?
???????? public void start ()
???????? 開始計時。不會影響到由setBase(long)設(shè)置的基準(zhǔn)時間,僅顯示視圖。即使部件不顯示,計時器也會通過定時處理消息來工作。為了確保不發(fā)生資源泄漏,用戶應(yīng)確保每個start()方法都有對應(yīng)的stop()調(diào)用(譯者注:有一個start就有一個stop)。(譯者注:start只是顯示計時,實(shí)際上計時是從基準(zhǔn)時間開始的,所以通過stop停止計時若干秒后再start時,顯示的計時會突然跳到當(dāng)前顯示的計時后的若干秒后繼續(xù)計時,見此帖子。)
?
???????? public void stop ()
???????? 停止計時。不會影響到由setBase(long)設(shè)置的基準(zhǔn)時間,僅顯示視圖。這將停止消息發(fā)送,有效地釋放計時器運(yùn)行時start()占用的資源。
?
受保護(hù)方法
???????? protected void onDetachedFromWindow ()
???????? 視圖從窗體上移除時調(diào)用,同時窗體表面不再顯示視圖。
?
???????? protected void onWindowVisibilityChanged (int visibility)
???????? 當(dāng)窗體中視圖的可視性(GONE, INVISIBLE, VISIBLE)發(fā)生改變時調(diào)用。注意它將告訴你你的窗口是否可以被窗口管理器識別,這并不能說明窗口是否被屏幕上的其他窗口遮擋,即使它本身是可見的。
?????????????????? 參數(shù)
??????????????????????????? visibility 窗口新的可見性
?
補(bǔ)充
???????? 文章鏈接
?????????????????? android中的時間服務(wù)–Chronometer計時器服務(wù)
???????? 示例代碼
?????????????????? Java文件
?????????????????? public class ChronometerDemo extends Activity {
??? ?????? private Chronometer cher1;
??? ?????? @Override
??? ?????? protected void onCreate(Bundle savedInstanceState) {
?????? ?????? super.onCreate(savedInstanceState);
?????? ?????? setContentView(R.layout.chronometer);
?????? ?????? cher1 = (Chronometer) findViewById(R.id.cher1);
?????? ?????? cher1.setFormat("計時:%s");
??? ?????? }
??? ??????
??? ?????? public void onStart(View view) {
?????? ?????? cher1.start();
??? ?????? }
??? ??????
??? ?????? public void onStop(View view) {
?????? ?????? cher1.stop();
??? ?????? }
??? ??????
??? ?????? public void onReset(View view) {
?????? ?????? cher1.setBase(SystemClock.elapsedRealtime());
??? ?????? }
}
?????????????????? XML文件
?????????????????? <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
??? android:layout_width="wrap_content" android:layout_height="wrap_content">
??? <Chronometer android:id="@+id/cher1" android:layout_width="wrap_content"
?????? android:layout_height="wrap_content"></Chronometer>
??? <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
?????? <Button android:onClick="onStart" android:text="開始計時" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
?????? <Button android:onClick="onStop" android:text="停止計時" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
?????? <Button android:onClick="onReset" android:text="重置" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>??
??? </LinearLayout>
</LinearLayout>