編輯:關於 Android 程式設計
Chronometer 是 Android 內建的計時元件,本篇用一個「設定秒數後開始倒數、時間到就跳提醒」的小案例,把它的核心 API 全部走過一遍。

整個流程只有三個關鍵動作:
1. 用 setBase(long base) 指定計時起點,實務上幾乎都傳入 SystemClock.elapsedRealtime()。
2. 註冊 setOnChronometerTickListener(),計時器每次刷新都會回呼一次,在裡面判斷「已走時間是否超過設定值」。
3. 條件成立時呼叫 stop() 停止計時,並彈出提示。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/back"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:orientation="horizontal">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:gravity="center"
android:text="設置時間:" />
<EditText
android:id="@+id/edt_settime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number" />
</LinearLayout>
<Chronometer
android:id="@+id/chronometer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#ff0000"
android:textSize="60dip" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:orientation="horizontal">
<Button
android:id="@+id/btnStart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="開始記時" />
<Button
android:id="@+id/btnStop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="停止記時" />
<Button
android:id="@+id/btnReset"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="重置" />
</LinearLayout>
</LinearLayout>
package com.jiahui.chronometer;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.EditText;
public class ChronometerDemoActivity extends Activity {
/** 使用者設定的秒數 */
private int startTime = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Chronometer chronometer = (Chronometer) findViewById(R.id.chronometer);
Button btnStart = (Button) findViewById(R.id.btnStart);
Button btnStop = (Button) findViewById(R.id.btnStop);
Button btnRest = (Button) findViewById(R.id.btnReset);
final EditText edtSetTime = (EditText) findViewById(R.id.edt_settime);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String ss = edtSetTime.getText().toString();
if (!ss.equals("")) {
startTime = Integer.parseInt(ss);
}
// 以「現在」作為計時基準點
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chronometer.stop();
}
});
btnRest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chronometer.setBase(SystemClock.elapsedRealtime());
}
});
chronometer.setOnChronometerTickListener(
new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer c) {
// 已走毫秒數 - 起點 = 經過時間
if (SystemClock.elapsedRealtime() - c.getBase()
> startTime * 1000) {
c.stop();
showDialog();
}
}
});
}
private void showDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.eb28d25);
builder.setTitle("警告")
.setMessage("時間到")
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create().show();
}
}
Chronometer 的內部計時依賴 SystemClock.elapsedRealtime(),它從開機起累加,且不受使用者手動改系統時間影響,因此計時不會因為時鐘校正而跳動。相對地,System.currentTimeMillis() 會被 NTP 校時或使用者改時區/時間影響,不適合拿來算間隔。
另外提醒一個實務細節:onChronometerTick 的呼叫頻率大約每秒一次,不要在裡面做重運算或 I/O;若需要更精細的控制,改用 Handler 或 CountDownTimer 會更合適。
基於 Android 實作點擊按鈕從其周圍彈出菜單選項(PopupMenu)
Android Material Design:PopupMenu Android Material Design 引入的 PopupMenu 類似於過去的上下文菜單,但
Android 事件響應的兩種模型:監聽 vs 回調
為什麼事件模型值得認真理解 對 Android 這種「頁程式」而言,事件響應是與界面編程綁在一起的核心知識:Activity 負責提供 UI,而事件響應負責讓 UI 真正
自定義控制項:復刻舊版優酷的三級旋轉菜單
效果預覽: 舊版優酷那個「點一下、面板翻轉展開」的側邊菜單, pernah 讓不少人想自己動手做一個。拆解之後其實不難,核心只有兩件事:三層重疊的 Relative
Android DiskLruCache 使用案例:打造 App 的磁碟快取
DiskLruCache 是 Google 官方推薦用來實作硬碟快取的類別。本文以「圖片快取」為例,把它的基本用法整理一遍:建立快取、寫入、讀取、清除與查詢大小。 效果