編輯:關於 Android 程式設計
Chronometer 是一個簡單的計時器,你可以給它一個開始時間並以此計時,或者如果你不給它開始時間,它將會使用你的通話開始時間。預設情況下它會以「分:秒」或「時:分:秒」的形式顯示當前計時器的值,或者可以用 setFormat(字串) 將計時器值格式化為任意字串。
1. 重要屬性
android:format:定義時間的格式,如 hh:mm:ss
2. 重要方法
setBase(long base):設置計時基準
setFormat(String format):設置顯示時間的格式。
start():開始計時
stop():停止計時
setOnChronometerTickListener(Chronometer.OnChronometerTickListener listener):當計時器改變時調用。
3. 範例
佈局檔案
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip" android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <Chronometer android:id="@+id/chronometer" android:format="Initial format: " android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0" android:paddingBottom="30dip" android:paddingTop="30dip" /> <Button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="開始"> <requestFocus /> </Button> <Button android:id="@+id/stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止"> </Button> <Button android:id="@+id/reset" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="重置"> </Button> <Button android:id="@+id/set_format" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="設置格式"> </Button> <Button android:id="@+id/clear_format" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清除格式"> </Button> </LinearLayout>
主程式:
package wjq.WidgetDemo; import android.app.Activity; import android.os.Bundle; import android.os.SystemClock; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Chronometer; public class ChronometerDemo extends Activity { private Chronometer mChronometer; /* (non-Javadoc) * @see android.app.Activity#onCreate(android.os.Bundle) */ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.chronometerpage); Button button; mChronometer = (Chronometer) findViewById(R.id.chronometer); // Watch for button clicks. button = (Button) findViewById(R.id.start); button.setOnClickListener(mStartListener); button = (Button) findViewById(R.id.stop); button.setOnClickListener(mStopListener); button = (Button) findViewById(R.id.reset); button.setOnClickListener(mResetListener); button = (Button) findViewById(R.id.set_format); button.setOnClickListener(mSetFormatListener); button = (Button) findViewById(R.id.clear_format); button.setOnClickListener(mClearFormatListener); } View.OnClickListener mStartListener = new OnClickListener() { public void onClick(View v) { mChronometer.start(); } }; View.OnClickListener mStopListener = new OnClickListener() { public void onClick(View v) { mChronometer.stop(); } }; View.OnClickListener mResetListener = new OnClickListener() { public void onClick(View v) { mChronometer.setBase(SystemClock.elapsedRealtime()); } }; View.OnClickListener mSetFormatListener = new OnClickListener() { public void onClick(View v) { mChronometer.setFormat("Formatted time (%s)"); } }; View.OnClickListener mClearFormatListener = new OnClickListener() { public void onClick(View v) { mChronometer.setFormat(null); } }; } 以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。
Android 帶索引側邊列的聯絡人清單實作全解
微信式的聯絡人清單,右側一條 A–Z 索引,滑到哪个字母就跳到對應位置——這個效果看起來 sophisticated,拆解之後其實只有四個步驟:轉拼音、排序、記錄位置、繪
Android Intent 與 Intent-filter 完全解析
Intent 是什麼 Intent 是 Android 元件之間溝通的意圖封裝。無論啟動 Activity、Service 或 BroadcastReceiver,都可透
Android 定位策略指南:兼顧精度與電量的取捨
本文所述的策略適用於平台內建的定位 API(android.location)。若使用 Google Play 服務中的融合定位 API(Fused Location P
Android 自定義數字解鎖鍵盤實作詳解
Android APP 數字上鎖 最近抽空做了一個數字解鎖的功能。手機有數字解鎖,App 同樣可以做到,避免某些應用的隱私洩漏。先看實現效果圖: 序言:這兩天接到一個