Android時間計時器控件Chronometer

  當你想要顯示時間的推移而不是進程的增加的時候,可以使用Chronometer作為一個計時器。當需要記錄用戶進行某項操作所消耗的時間的時候,或者在遊戲中需要限制時間時候,它將非常實用。

       這裡,Chronometer對象的format屬性可以用來設置顯示時間周圍的文字。並且隻有它的start()在被調用之後才會顯示時間及附加文字,要停止計時器可以調用stop()方法。setBase()方法用來設定設置計時器的起點,當將計時器幾點設置為0的時候,計時器將從手機上次重啟的時刻開始計時。

方法簡介:

   long getBase(); //返回基地的時間,由setBase(long)設置的,可以是SystemClock.elapsedRealtime() 

   String getFormat();//返回當前字符串格式,此格式是通過setFormat()實現的
   void setBase(long base);//設置時間,計數定時器指定的值
   void setFormat(String format);//設置顯示的內容,計時器將會顯示這個參數所對應的值得,如果字符串的值
                                                為null,那麼返回的值為MM:SS格式的

 

chronometer是一個計時器,我們可以通過其進行設置。主要的方法有

 

Chronometer類有3個重要的方法:start、stop和setBase,其中start方法表示開始計時;stop表示停止計時;setBase方法表示重新計時。start和stop方法沒有任何參數,setBase方法有參數,表示開始計時的基準時間。如果要從當前時刻重新計時,可以將該參數值設為temClock.elapsedRealtime()。

還可以對Chronometer組件做進一步設置。在默認情況下,Chronometer組件隻輸出MM:SS:MM:SS的時間格式。例如,當計時到1分20秒時,Chronometer組件會顯示01:20。如果變顯示的信息內容,可以使用Chronometer類的setFormat方法。該方法需要一個String變並使用“%s”表示計時信息。例如,使用setFormat(“計時信息:%s”)設置顯示信息,Chronometer會顯示計時信息:(計時信息:10:20)。

Chronometer組件還可以通過onChronometerTick事件方法來捕捉計時動作。該方法1秒調一次。要想使用onChronometerTick事件方法,必須實現接口:android.widget.Chronometer.OnChronometerTickListener

ChronometerDemo.java

[java]
<span style="color:#000000"><strong>public</strong></span> <span style="color:#000000"><strong>class</strong></span> ChronometerDemo <span style="color:#000000"><strong>extends</strong></span> Activity <span style="color:#009900">{</span> 
    <span style="color:#000000"><strong>private</strong></span> Chronometer mChronometer<span style="color:#339933">;</span><span style="color:#666666"><em>//創建一個計時器對象</em></span> 
    <span style="color:#000000"><strong>private</strong></span> <span style="color:#003399">Button</span> button<span style="color:#339933">;</span> 
    @Override 
    <span style="color:#000000"><strong>protected</strong></span> <span style="color:#000066"><strong>void</strong></span> onCreate<span style="color:#009900">(</span>Bundle savedInstanceState<span style="color:#009900">)</span> <span style="color:#009900">{</span> 
        <span style="color:#000000"><strong>super</strong></span>.<span style="color:#006633">onCreate</span><span style="color:#009900">(</span>savedInstanceState<span style="color:#009900">)</span><span style="color:#339933">;</span> 
        setContentView<span style="color:#009900">(</span>R.<span style="color:#006633">layout</span>.<span style="color:#006633">chronometer</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
        mChronometer <span style="color:#339933">=</span> <span style="color:#009900">(</span>Chronometer<span style="color:#009900">)</span> findViewById<span style="color:#009900">(</span>R.<span style="color:#006633">id</span>.<span style="color:#006633">chronometer</span><span style="color:#009900">)</span><span style="color:#339933">;</span><span style="color:#666666"><em>//將計時器對象實體化</em></span> 
    <span style="color:#009900">}</span> 
    <span style="color:#008000"><strong><em>/**初始化按鈕Button組件*/</em></strong></span> 
    <span style="color:#000000"><strong>public</strong></span> <span style="color:#000066"><strong>void</strong></span> initWidget<span style="color:#009900">(</span><span style="color:#009900">)</span><span style="color:#009900">{</span> 
        <span style="color:#666666"><em>// Watch for button clicks.</em></span> 
        button <span style="color:#339933">=</span> <span style="color:#009900">(</span><span style="color:#003399">Button</span><span style="color:#009900">)</span> findViewById<span style="color:#009900">(</span>R.<span style="color:#006633">id</span>.<span style="color:#006633">start</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
        button.<span style="color:#006633">setOnClickListener</span><span style="color:#009900">(</span>mStartListener<span style="color:#009900">)</span><span style="color:#339933">;</span> 
  
        button <span style="color:#339933">=</span> <span style="color:#009900">(</span><span style="color:#003399">Button</span><span style="color:#009900">)</span> findViewById<span style="color:#009900">(</span>R.<span style="color:#006633">id</span>.<span style="color:#006633">stop</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
        button.<span style="color:#006633">setOnClickListener</span><span style="color:#009900">(</span>mStopListener<span style="color:#009900">)</span><span style="color:#339933">;</span> 
  
        button <span style="color:#339933">=</span> <span style="color:#009900">(</span><span style="color:#003399">Button</span><span style="color:#009900">)</span> findViewById<span style="color:#009900">(</span>R.<span style="color:#006633">id</span>.<span style="color:#006633">reset</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
        button.<span style="color:#006633">setOnClickListener</span><span style="color:#009900">(</span>mResetListener<span style="color:#009900">)</span><span style="color:#339933">;</span> 
  
        button <span style="color:#339933">=</span> <span style="color:#009900">(</span><span style="color:#003399">Button</span><span style="color:#009900">)</span> findViewById<span style="color:#009900">(</span>R.<span style="color:#006633">id</span>.<span style="color:#006633">set_format</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
        button.<span style="color:#006633">setOnClickListener</span><span style="color:#009900">(</span>mSetFormatListener<span style="color:#009900">)</span><span style="color:#339933">;</span> 
  
        button <span style="color:#339933">=</span> <span style="color:#009900">(</span><span style="color:#003399">Button</span><span style="color:#009900">)</span> findViewById<span style="color:#009900">(</span>R.<span style="color:#006633">id</span>.<span style="color:#006633">clear_format</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
        button.<span style="color:#006633">setOnClickListener</span><span style="color:#009900">(</span>mClearFormatListener<span style="color:#009900">)</span><span style="color:#339933">;</span> 
    <span style="color:#009900">}</span> 
  
    <span style="color:#003399">View</span>.<span style="color:#006633">OnClickListener</span> mStartListener <span style="color:#339933">=</span> <span style="color:#000000"><strong>new</strong></span> OnClickListener<span style="color:#009900">(</span><span style="color:#009900">)</span> <span style="color:#009900">{</span> 
        <span style="color:#000000"><strong>public</strong></span> <span style="color:#000066"><strong>void</strong></span> onClick<span style="color:#009900">(</span><span style="color:#003399">View</span> v<span style="color:#009900">)</span> <span style="color:#009900">{</span> 
            mChronometer.<span style="color:#006633">start</span><span style="color:#009900">(</span><span style="color:#009900">)</span><span style="color:#339933">;</span><span style="color:#666666"><em>//計時器開始</em></span> 
        <span style="color:#009900">}</span> 
    <span style="color:#009900">}</span><span style="color:#339933">;</span> 
  
    <span style="color:#003399">View</span>.<span style="color:#006633">OnClickListener</span> mStopListener <span style="color:#339933">=</span> <span style="color:#000000"><strong>new</strong></span> OnClickListener<span style="color:#009900">(</span><span style="color:#009900">)</span> <span style="color:#009900">{</span> 
        <span style="color:#000000"><strong>public</strong></span> <span style="color:#000066"><strong>void</strong></span> onClick<span style="color:#009900">(</span><span style="color:#003399">View</span> v<span style="color:#009900">)</span> <span style="color:#009900">{</span> 
            mChronometer.<span style="color:#006633">stop</span><span style="color:#009900">(</span><span style="color:#009900">)</span><span style="color:#339933">;</span><span style="color:#666666"><em>//計時器暫停</em></span> 
        <span style="color:#009900">}</span> 
    <span style="color:#009900">}</span><span style="color:#339933">;</span> 
  
    <span style="color:#003399">View</span>.<span style="color:#006633">OnClickListener</span> mResetListener <span style="color:#339933">=</span> <span style="color:#000000"><strong>new</strong></span> OnClickListener<span style="color:#009900">(</span><span style="color:#009900">)</span> <span style="color:#009900">{</span> 
        <span style="color:#000000"><strong>public</strong></span> <span style="color:#000066"><strong>void</strong></span> onClick<span style="color:#009900">(</span><span style="color:#003399">View</span> v<span style="color:#009900">)</span> <span style="color:#009900">{</span> 
            <span style="color:#666666"><em>//計時器重置為0</em></span> 
            mChronometer.<span style="color:#006633">setBase</span><span style="color:#009900">(</span>SystemClock.<span style="color:#006633">elapsedRealtime</span><span style="color:#009900">(</span><span style="color:#009900">)</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
        <span style="color:#009900">}</span> 
    <span style="color:#009900">}</span><span style="color:#339933">;</span> 
  
    <span style="color:#003399">View</span>.<span style="color:#006633">OnClickListener</span> mSetFormatListener <span style="color:#339933">=</span> <span style="color:#000000"><strong>new</strong></span> OnClickListener<span style="color:#009900">(</span><span style="color:#009900">)</span> <span style="color:#009900">{</span> 
        <span style="color:#000000"><strong>public</strong></span> <span style="color:#000066"><strong>void</strong></span> onClick<span style="color:#009900">(</span><span style="color:#003399">View</span> v<span style="color:#009900">)</span> <span style="color:#009900">{</span> 
            <span style="color:#666666"><em>//改變計時器顯示內容方式</em></span> 
            mChronometer.<span style="color:#006633">setFormat</span><span style="color:#009900">(</span><span style="color:#0000ff">"Formatted time (%s)"</span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
        <span style="color:#009900">}</span> 
    <span style="color:#009900">}</span><span style="color:#339933">;</span> 
  
    <span style="color:#003399">View</span>.<span style="color:#006633">OnClickListener</span> mClearFormatListener <span style="color:#339933">=</span> <span style="color:#000000"><strong>new</strong></span> OnClickListener<span style="color:#009900">(</span><span style="color:#009900">)</span> <span style="color:#009900">{</span> 
        <span style="color:#000000"><strong>public</strong></span> <span style="color:#000066"><strong>void</strong></span> onClick<span style="color:#009900">(</span><span style="color:#003399">View</span> v<span style="color:#009900">)</span> <span style="color:#009900">{</span> 
            <span style="color:#666666"><em>//恢復原來計時器顯示方式</em></span> 
            mChronometer.<span style="color:#006633">setFormat</span><span style="color:#009900">(</span><span style="color:#000066"><strong>null</strong></span><span style="color:#009900">)</span><span style="color:#339933">;</span> 
        <span style="color:#009900">}</span> 
    <span style="color:#009900">}</span><span style="color:#339933">;</span> 
<span style="color:#009900">}</span> 
chronometer.xml

[xml]
<span style="color:#009900"><span style="color:#000000"><strong><?xml</strong></span> <span style="color:#000066">version</span>=<span style="color:#ff0000">"1.0"</span> <span style="color:#000066">encoding</span>=<span style="color:#ff0000">"utf-8"</span><span style="color:#000000"><strong>?></strong></span></span> 
<span style="color:#009900"><span style="color:#000000"><strong><LinearLayout</strong></span> <span style="color:#000066">xmlns:android</span>=<span style="color:#ff0000">"http://schemas.android.com/apk/res/android"</span></span> 
<span style="color:#009900">    <span style="color:#000066">android:orientation</span>=<span style="color:#ff0000">"vertical"</span> <span style="color:#000066">android:padding</span>=<span style="color:#ff0000">"4dip"</span> <span style="color:#000066">android:gravity</span>=<span style="color:#ff0000">"center_horizontal"</span></span> 
<span style="color:#009900">    <span style="color:#000066">android:layout_width</span>=<span style="color:#ff0000">"fill_parent"</span> <span style="color:#000066">android:layout_height</span>=<span style="color:#ff0000">"fill_parent"</span><span style="color:#000000"><strong>></strong></span></span> 
    <span style="color:#009900"><span style="color:#000000"><strong><Chronometer</strong></span> <span style="color:#000066">android:id</span>=<span style="color:#ff0000">"@+id/chronometer"</span></span> 
<span style="color:#009900">        <span style="color:#000066">android:format</span>=<span style="color:#ff0000">"@string/chronometer_initial_format"</span></span> 
<span style="color:#009900">        <span style="color:#000066">android:layout_width</span>=<span style="color:#ff0000">"wrap_content"</span> <span style="color:#000066">android:layout_height</span>=<span style="color:#ff0000">"wrap_content"</span></span> 
<span style="color:#009900">        <span style="color:#000066">android:layout_weight</span>=<span style="color:#ff0000">"0"</span> <span style="color:#000066">android:paddingBottom</span>=<span style="color:#ff0000">"30dip"</span></span> 
<span style="color:#009900">        <span style="color:#000066">android:paddingTop</span>=<span style="color:#ff0000">"30dip"</span> <span style="color:#000000"><strong>/></strong></span></span> 
    <span style="color:#009900"><span style="color:#000000"><strong><Button</strong></span> <span style="color:#000066">android:id</span>=<span style="color:#ff0000">"@+id/start"</span> <span style="color:#000066">android:layout_width</span>=<span style="color:#ff0000">"wrap_content"</span></span> 
<span style="color:#009900">        <span style="color:#000066">android:layout_height</span>=<span style="color:#ff0000">"wrap_content"</span> <span style="color:#000066">android:text</span>=<span style="color:#ff0000">"@string/chronometer_start"</span><span style="color:#000000"><strong>></strong></span></span> 
        <span style="color:#009900"><span style="color:#000000"><strong><requestFocus</strong></span> <span style="color:#000000"><strong>/></strong></span></span> 
    <span style="color:#009900"><span style="color:#000000"><strong></Button<span style="color:#000000"><strong>></strong></span></strong></span></span> 
    <span style="color:#009900"><span style="color:#000000"><strong><Button</strong></span> <span style="color:#000066">android:id</span>=<span style="color:#ff0000">"@+id/stop"</span> <span style="color:#000066">android:layout_width</span>=<span style="color:#ff0000">"wrap_content"</span></span> 
<span style="color:#009900">        <span style="color:#000066">android:layout_height</span>=<span style="color:#ff0000">"wrap_content"</span> <span style="color:#000066">android:text</span>=<span style="color:#ff0000">"@string/chronometer_stop"</span><span style="color:#000000"><strong>></strong></span></span> 
    <span style="color:#009900"><span style="color:#000000"><strong></Button<span style="color:#000000"><strong>></strong></span></strong></span></span> 
    <span style="color:#009900"><span style="color:#000000"><strong><Button</strong></span> <span style="color:#000066">android:id</span>=<span style="color:#ff0000">"@+id/reset"</span> <span style="color:#000066">android:layout_width</span>=<span style="color:#ff0000">"wrap_content"</span></span> 
<span style="color:#009900">        <span style="color:#000066">android:layout_height</span>=<span style="color:#ff0000">"wrap_content"</span> <span style="color:#000066">android:text</span>=<span style="color:#ff0000">"@string/chronometer_reset"</span><span style="color:#000000"><strong>></strong></span></span> 
    <span style="color:#009900"><span style="color:#000000"><strong></Button<span style="color:#000000"><strong>></strong></span></strong></span></span> 
    <span style="color:#009900"><span style="color:#000000"><strong><Button</strong></span> <span style="color:#000066">android:id</span>=<span style="color:#ff0000">"@+id/set_format"</span> <span style="color:#000066">android:layout_width</span>=<span style="color:#ff0000">"wrap_content"</span></span> 
<span style="color:#009900">        <span style="color:#000066">android:layout_height</span>=<span style="color:#ff0000">"wrap_content"</span> <span style="color:#000066">android:text</span>=<span style="color:#ff0000">"@string/chronometer_set_format"</span><span style="color:#000000"><strong>></strong></span></span> 
    <span style="color:#009900"><span style="color:#000000"><strong></Button<span style="color:#000000"><strong>></strong></span></strong></span></span> 
    <span style="color:#009900"><span style="color:#000000"><strong><Button</strong></span> <span style="color:#000066">android:id</span>=<span style="color:#ff0000">"@+id/clear_format"</span> <span style="color:#000066">android:layout_width</span>=<span style="color:#ff0000">"wrap_content"</span></span> 
<span style="color:#009900">        <span style="color:#000066">android:layout_height</span>=<span style="color:#ff0000">"wrap_content"</span> <span style="color:#000066">android:text</span>=<span style="color:#ff0000">"@string/chronometer_clear_format"</span><span style="color:#000000"><strong>></strong></span></span> 
    <span style="color:#009900"><span style="color:#000000"><strong></Button<span style="color:#000000"><strong>></strong></span></strong></span></span> 
<span style="color:#009900"><span style="color:#000000"><strong></LinearLayout<span style="color:#000000"><strong>></strong></span></strong></span></span> 
string.xml

[xml]
<span style="color:#009900"><span style="color:#000000"><strong><?xml</strong></span> <span style="color:#000066">version</span>=<span style="color:#ff0000">"1.0"</span> <span style="color:#000066">encoding</span>=<span style="color:#ff0000">"utf-8"</span><span style="color:#000000"><strong>?></strong></span></span> 
<span style="color:#009900"><span style="color:#000000"><strong><resources</strong></span> <span style="color:#000066">xmlns:xliff</span>=<span style="color:#ff0000">"urn:oasis:names:tc:xliff:document:1.2"</span><span style="color:#000000"><strong>></strong></span></span> 
    <span style="color:#009900"><span style="color:#000000"><strong><string</strong></span> <span style="color:#000066">name</span>=<span style="color:#ff0000">"hello"</span><span style="color:#000000"><strong>></strong></span></span>Hello World, ChronometerDemo!<span style="color:#009900"><span style="color:#000000"><strong></string<span style="color:#000000"><strong>></strong></span></strong></span></span> 
    <span style="color:#009900"><span style="color:#000000"><strong><string</strong></span> <span style="color:#000066">name</span>=<span style="color:#ff0000">"app_name"</span><span style="color:#000000"><strong>></strong></span></span>ChronometerDemo<span style="color:#009900"><span style="color:#000000"><strong></string<span style="color:#000000"><strong>></strong></span></strong></span></span> 
    <span style="color:#009900"><span style="color:#000000"><strong><string</strong></span> <span style="color:#000066">name</span>=<span style="color:#ff0000">"chronometer_start"</span><span style="color:#000000"><strong>></strong></span></span>Start<span style="color:#009900"><span style="color:#000000"><strong></string<span style="color:#000000"><strong>></strong></span></strong></span></span> 
    <span style="color:#009900"><span style="color:#000000"><strong><string</strong></span> <span style="color:#000066">name</span>=<span style="color:#ff0000">"chronometer_stop"</span><span style="color:#000000"><strong>></strong></span></span>Stop<span style="color:#009900"><span style="color:#000000"><strong></string<span style="color:#000000"><strong>></strong></span></strong></span></span> 
    <span style="color:#009900"><span style="color:#000000"><strong><string</strong></span> <span style="color:#000066">name</span>=<span style="color:#ff0000">"chronometer_reset"</span><span style="color:#000000"><strong>></strong></span></span>Reset<span style="color:#009900"><span style="color:#000000"><strong></string<span style="color:#000000"><strong>></strong></span></strong></span></span> 
    <span style="color:#009900"><span style="color:#000000"><strong><string</strong></span> <span style="color:#000066">name</span>=<span style="color:#ff0000">"chronometer_set_format"</span><span style="color:#000000"><strong>></strong></span></span>Set format string<span style="color:#009900"><span style="color:#000000"><strong></string<span style="color:#000000"><strong>></strong></span></strong></span></span> 
    <span style="color:#009900"><span style="color:#000000"><strong><string</strong></span> <span style="color:#000066">name</span>=<span style="color:#ff0000">"chronometer_clear_format"</span><span style="color:#000000"><strong>></strong></span></span>Clear format string<span style="color:#009900"><span style="color:#000000"><strong></string<span style="color:#000000"><strong>></strong></span></strong></span></span> 
    <span style="color:#009900"><span style="color:#000000"><strong><string</strong></span> <span style="color:#000066">name</span>=<span style="color:#ff0000">"chronometer_initial_format"</span><span style="color:#000000"><strong>></strong></span></span>Initial format: <span style="color:#009900"><span style="color:#000000"><strong><xliff:g</strong></span> <span style="color:#000066">id</span>=<span style="color:#ff0000">"initial-format"</span><span style="color:#000000"><strong>></strong></span></span>%s<span style="color:#009900"><span style="color:#000000"><strong></xliff:g<span style="color:#000000"><strong>></strong></span></strong></span><span style="color:#000000"><strong></string<span style="color:#000000"><strong>></strong></span></strong></span></span> 
<span style="color:#009900"><span style

 

摘自 圖靈的夢

發佈留言