?
Dokumen ini menggunakan Manual laman web PHP Cina Lepaskan
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)了一個(gè)簡(jiǎn)單的計(jì)時(shí)器。
你可以通過(guò)elapsedRealtime()
來(lái)
給它一個(gè)基準(zhǔn)時(shí)間,并從該時(shí)間開(kāi)始計(jì)數(shù)。如果你不給它基準(zhǔn)時(shí)間,它將使用你調(diào)用start()
時(shí)的時(shí)間。默認(rèn)它將顯示當(dāng)前"MM:SS"或 "H:MM:SS"格式的時(shí)間,或者你能通過(guò)setFormat(String)
設(shè)置一個(gè)任意字符串來(lái)格式化顯示計(jì)時(shí)器顯示的時(shí)間。
?
XML屬性
屬性名稱 |
描述 |
android:format |
格式化字符串:如果指定,計(jì)時(shí)器將根據(jù)這個(gè)字符串來(lái)顯示,替換字符串中第一個(gè)“%s”為當(dāng)前"MM:SS"或 "H:MM:SS"格式的時(shí)間顯示。如果不指定,計(jì)時(shí)器將簡(jiǎn)單的顯示"MM:SS"
or "H:MM:SS"格式的時(shí)間。(譯者注:如: |
?
構(gòu)造函數(shù)
???????? public Chronometer (Context context)
初始化計(jì)時(shí)器對(duì)象。設(shè)置當(dāng)前時(shí)間為基準(zhǔn)時(shí)間。(譯者注:通過(guò)程序動(dòng)態(tài)創(chuàng)建計(jì)時(shí)器對(duì)象)
?
public Chronometer (Context context, AttributeSet attrs)
初始化標(biāo)準(zhǔn)視圖布局信息。設(shè)置當(dāng)前時(shí)間為基準(zhǔn)時(shí)間。(譯者注:指通過(guò)XML來(lái)指定一個(gè)計(jì)時(shí)器)
?
public Chronometer (Context context, AttributeSet attrs, int defStyle)
初始化標(biāo)準(zhǔn)視圖布局信息和風(fēng)格。設(shè)置當(dāng)前時(shí)間為基準(zhǔn)時(shí)間。
?
公共方法???????
???????? public long getBase ()
???????? 返回先前由setBase(long)設(shè)置的基準(zhǔn)時(shí)間。
?
???????? public String getFormat ()
???????? 返回先前由setFormat(String)設(shè)置的格式化字符串。
?
???????? public Chronometer.OnChronometerTickListener getOnChronometerTickListener ()
?????????????????? 返回值
??????????????????????????? 返回這個(gè)監(jiān)聽(tīng)器(可能為空)是用于監(jiān)聽(tīng)計(jì)時(shí)器變化的事件。
?
???????? public void setBase (long base)
???????? 設(shè)置基準(zhǔn)時(shí)間(譯者注:基準(zhǔn)時(shí)間為真正意義上開(kāi)始計(jì)時(shí)的時(shí)間,而不是調(diào)用start時(shí)時(shí)間,比如調(diào)用本函數(shù)并設(shè)置參數(shù)base為SystemClock.elapsedRealtime()即表示從當(dāng)前時(shí)間開(kāi)始重新計(jì)時(shí))。
?????????????????? 參數(shù)
??????????????????????????? base ?????? 使用elapsedRealtime()為基準(zhǔn)時(shí)間
?
???????? public void setFormat (String format)
???????? 設(shè)置用于顯示的格式化字符串。格式化字符串:如果指定,計(jì)時(shí)器將根據(jù)這個(gè)字符串來(lái)顯示,替換字符串中第一個(gè)“%s”為當(dāng)前"MM:SS"或 "H:MM:SS"格式的時(shí)間顯示。如果這個(gè)格式化字符串為空,或者你從未調(diào)用過(guò)setFormat()方法,計(jì)時(shí)器將簡(jiǎn)單的顯示"MM:SS" or
"H:MM:SS"格式的時(shí)間。(譯者注:如:"This is a Chronometer %s"
)
?????????????????? 參數(shù)
??????????????????????????? format ?? 格式化字符串
?
???????? public void setOnChronometerTickListener (Chronometer.OnChronometerTickListener listener)
???????? 設(shè)置計(jì)時(shí)器變化時(shí)調(diào)用的監(jiān)聽(tīng)事件。
?????????????????? 參數(shù)
??????????????????????????? listener ? The listener.
?
???????? public void start ()
???????? 開(kāi)始計(jì)時(shí)。不會(huì)影響到由setBase(long)設(shè)置的基準(zhǔn)時(shí)間,僅顯示視圖。即使部件不顯示,計(jì)時(shí)器也會(huì)通過(guò)定時(shí)處理消息來(lái)工作。為了確保不發(fā)生資源泄漏,用戶應(yīng)確保每個(gè)start()方法都有對(duì)應(yīng)的stop()調(diào)用(譯者注:有一個(gè)start就有一個(gè)stop)。(譯者注:start只是顯示計(jì)時(shí),實(shí)際上計(jì)時(shí)是從基準(zhǔn)時(shí)間開(kāi)始的,所以通過(guò)stop停止計(jì)時(shí)若干秒后再start時(shí),顯示的計(jì)時(shí)會(huì)突然跳到當(dāng)前顯示的計(jì)時(shí)后的若干秒后繼續(xù)計(jì)時(shí),見(jiàn)此帖子。)
?
???????? public void stop ()
???????? 停止計(jì)時(shí)。不會(huì)影響到由setBase(long)設(shè)置的基準(zhǔn)時(shí)間,僅顯示視圖。這將停止消息發(fā)送,有效地釋放計(jì)時(shí)器運(yùn)行時(shí)start()占用的資源。
?
受保護(hù)方法
???????? protected void onDetachedFromWindow ()
???????? 視圖從窗體上移除時(shí)調(diào)用,同時(shí)窗體表面不再顯示視圖。
?
???????? protected void onWindowVisibilityChanged (int visibility)
???????? 當(dāng)窗體中視圖的可視性(GONE, INVISIBLE, VISIBLE)發(fā)生改變時(shí)調(diào)用。注意它將告訴你你的窗口是否可以被窗口管理器識(shí)別,這并不能說(shuō)明窗口是否被屏幕上的其他窗口遮擋,即使它本身是可見(jiàn)的。
?????????????????? 參數(shù)
??????????????????????????? visibility 窗口新的可見(jiàn)性
?
補(bǔ)充
???????? 文章鏈接
?????????????????? android中的時(shí)間服務(wù)–Chronometer計(jì)時(shí)器服務(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("計(jì)時(shí):%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="開(kāi)始計(jì)時(shí)" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
?????? <Button android:onClick="onStop" android:text="停止計(jì)時(shí)" 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>