Chronometr是一個簡單的定時器,你可以給它一個開始時間,並以此定時,或者如果你不給它一個開始時間,
它將會使用你的時間通話開始。預設情況下它會顯示在當前定時器的值得形式"分:秒" 或"MM:SS",
或者可以使用set(字串)的定時器值到一個任一字元串
重要屬性
android:format 定義時間的格式如: hh:mm:ss
重要方法
setBase(long base)//設定倒計時定時器setFormat(String format)//設定顯示時間的格式start()//開始計時stop()//停止計時 //當計時器改變時調用setOnChronometerTickListener(Chronometer.OnChronometerTickListener listener)
常式main.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/chronometer" android:layout_width="wrap_content" android:layout_height="wrap_content"/><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 android:onClick="onStop" android:text="停止計時" android:layout_width="wrap_content" android:layout_height="wrap_content"/><Button android:onClick="onReset" android:text="重設" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout></LinearLayout>
Java源碼
ackage com.sx.Chronometer;import android.app.Activity;import android.os.Bundle;import android.os.SystemClock;import android.view.View;import android.widget.Chronometer;public class ChronometerActivity extends Activity {private Chronometer chronometer = null;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); chronometer = (Chronometer) findViewById(R.id.chronometer); chronometer.setFormat("計時:%s");}public void onStart(View view) { chronometer.start();}public void onStop(View view) { chronometer.stop();}public void onReset(View view){ chronometer.setBase(SystemClock.elapsedRealtime());}}