天天看點

android 中文 api (43) —— Chronometer

正文

  一、結構

    public class Chronometer extends TextView

    java.lang.Object

        android.widget.Chronometer

  二、概述

    

  類實作了一個簡單的計時器。

  三、XML屬性

屬性名稱

描述

android:format

格式化字元串:如果指定,計時器将根據這個字元串來顯示,替換字元串中第一個“%s”為目前"MM:SS"或"H:MM:SS"格式的時間顯示。如果不指定,計時器将簡單的顯示"MM:SS" or "H:MM:SS"格式的時間。(譯者注:如:<code>“This is a Chronometer %s”</code>)

  四、構造函數

         public Chronometer (Context context)

  初始化計時器對象。設定目前時間為基準時間。(譯者注:通過程式動态建立計時器對象)

  public Chronometer (Context context, AttributeSet attrs)

  初始化标準視圖布局資訊。設定目前時間為基準時間。(譯者注:指通過XML來指定一個計時器)

  public Chronometer (Context context, AttributeSet attrs, int defStyle)

    初始化标準視圖布局資訊和風格。設定目前時間為基準時間。 

  五、公共方法

         public long getBase ()

         傳回先前由setBase(long)設定的基準時間。

         public String getFormat ()

         傳回先前由setFormat(String)設定的格式化字元串。

         public Chronometer.OnChronometerTickListener getOnChronometerTickListener ()

                   傳回值

                            傳回這個監聽器(可能為空)是用于監聽計時器變化的事件。

         public void setBase (long base)

         設定基準時間(譯者注:基準時間為真正意義上開始計時的時間,而不是調用start時時間,比如調用本函數并設定參數base為SystemClock.elapsedRealtime()即表示從目前時間開始重新計時)。

                   參數

                            base        使用elapsedRealtime()為基準時間

         public void setFormat (String format)

         設定用于顯示的格式化字元串。格式化字元串:如果指定,計時器将根據這個字元串來顯示,替換字元串中第一個“%s”為目前"MM:SS"或 "H:MM:SS"格式的時間顯示。如果這個格式化字元串為空,或者你從未調用過setFormat()方法,計時器将簡單的顯示"MM:SS" or "H:MM:SS"格式的時間。(譯者注:如:<code>"This is a Chronometer %s"</code>)

                            format    格式化字元串

         public void setOnChronometerTickListener(Chronometer.OnChronometerTickListener listener)

         設定計時器變化時調用的監聽事件。

                            listener  The listener.

         public void start ()

         public void stop ()

         停止計時。不會影響到由setBase(long)設定的基準時間,僅顯示視圖。這将停止消息發送,有效地釋放計時器運作時start()占用的資源。

  六、受保護方法

         protected void onDetachedFromWindow ()

         視圖從窗體上移除時調用,同時窗體表面不再顯示視圖。

         protected void onWindowVisibilityChanged (int visibility)

         當窗體中視圖的可視性(GONE, INVISIBLE, VISIBLE)發生改變時調用。注意它将告訴你你的視窗是否可以被視窗管理器識别,這并不能說明視窗是否被螢幕上的其他視窗遮擋,即使它本身是可見的。

                            visibility 視窗新的可見性

  七、補充

    文章連結

    示例代碼

      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");

            }

            /**

            * 開始計時

             * @param view

             */

            public void onStart(View view) {

                cher1.start();

             * 停止計時

            public void onStop(View view) {

                cher1.stop();

             * 重置

            public void onReset(View view) {

                cher1.setBase(SystemClock.elapsedRealtime());

    }

      XML檔案

        &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"

    android:layout_width="wrap_content" android:layout_height="wrap_content"&gt;

    &lt;Chronometer android:id="@+id/cher1" android:layout_width="wrap_content"

        android:layout_height="wrap_content"&gt;&lt;/Chronometer&gt;

    &lt;LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"&gt;

        &lt;Button android:onClick="onStart" android:text="開始計時" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt;&lt;/Button&gt;

        &lt;Button android:onClick="onStop" android:text="停止計時" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt;&lt;/Button&gt;

        &lt;Button android:onClick="onReset" android:text="重置" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt;&lt;/Button&gt;    

    &lt;/LinearLayout&gt;

&lt;/LinearLayout&gt;

本文轉自over140 51CTO部落格,原文連結:http://blog.51cto.com/over140/582602,如需轉載請自行聯系原作者

繼續閱讀