編輯:關於 Android 程式設計
本文為大家分享透過 Chronometer 實現倒計時功能,Android 提供了實現按秒計時的 API,供各位參考,具體內容如下。
一、自訂 ChronometerView,繼續自 TextView
主要原理:先設置一個基準倒計時時間 mBaseSeconds,內置 handler 每隔 1s 發送一個空訊息,mRemainSeconds--,同時刷新介面視圖,回呼給外部調用者,直到為零。外部調用者可透過 start()/pause()/stop() 來控制計時器的工作狀態。以 App 中發送簡訊驗證碼的場景為例,做了一個很粗糙的介面,但功能都實現了。

/** * @name 倒計時器(類似妙表倒數計時,支持暫停、停止、重新開始) * @author Fanjb * @date 2015年11月6日 */ public class ChronometerView extends TextView { /** * A callback that notifies when the chronometer has decremented on its own. * * @author Fanjb */ public interface OnTickChangeListener { /** * remain seconds changed * * @param view * @param remainSeconds */ public void onTickChanged(ChronometerView view, long remainSeconds); } private long mBase; private long mRemainSeconds; private boolean mStarted; private boolean mReStart; private boolean mVisible; private boolean mIsEnable; private OnTickChangeListener mTickListener; public ChronometerView(Context context) { this(context, null); } public ChronometerView(Context context, AttributeSet attrs) { super(context, attrs, 0); } public ChronometerView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); updateText(mRemainSeconds); } @Override protected void onWindowVisibilityChanged(int visibility) { super.onWindowVisibilityChanged(visibility); mVisible = visibility == VISIBLE; updateStatus(); } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); mVisible = false; updateStatus(); } /** * 啟動計時器 */ public void start() { if (mReStart && !mStarted) { mRemainSeconds = mBase; } mStarted = true; updateStatus(); } /** * 暫停計時器 */ public void pause() { if (mStarted) { mStarted = mReStart = false; updateStatus(); } } /** * 停止計時器,再次調用 start()重新啟動 */ public void stop() { mStarted = false; mReStart = true; updateStatus(); updateText(mRemainSeconds = 0); dispatchTickListener(); } /** * 刷新內部狀態 */ private void updateStatus() { boolean isEnable = mVisible && mStarted; if (mIsEnable != isEnable) { if (isEnable) { mHandler.sendMessage(Message.obtain(mHandler, TICK_WHAT)); } else { mHandler.removeMessages(TICK_WHAT); } mIsEnable = isEnable; } } private static final int TICK_WHAT = 1; private Handler mHandler = new Handler() { public void handleMessage(android.os.Message msg) { if (mRemainSeconds > 0) { updateText(--mRemainSeconds); dispatchTickListener(); sendMessageDelayed(Message.obtain(this, TICK_WHAT), 1000); } } }; private void updateText(long now) { String text = DateUtils.formatElapsedTime(now); setText(text); } /** * 在未啟動狀態下設置開始倒計時時間 * * @param baseSeconds */ public void setBaseSeconds(long baseSeconds) { if (baseSeconds > 0 && baseSeconds != mBase && !mStarted) { mBase = mRemainSeconds = baseSeconds; updateText(mRemainSeconds); } } /** * 剩余時間 * * @return */ public long getRemainSeconds() { return mRemainSeconds; } public void setOnTickChangeListener(OnTickChangeListener listener) { mTickListener = listener; } public OnTickChangeListener getTickListener() { return mTickListener; } private void dispatchTickListener() { if (mTickListener != null) { mTickListener.onTickChanged(this, getRemainSeconds()); } } @Override public void onInitializeAccessibilityEvent(AccessibilityEvent event) { super.onInitializeAccessibilityEvent(event); event.setClassName(ChronometerView.class.getName()); } @Override public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { super.onInitializeAccessibilityNodeInfo(info); info.setClassName(Chronometer.class.getName()); } }二、xml 中沒有加入自訂的控制項屬性,同 TextView
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <com.freedoman.widgets.calendar.ChronometerView android:id="@+id/chronometer_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:background="@drawable/chronometer_view_bg" android:enabled="true" android:text="00:00" /> <Button android:id="@+id/start_chronometer_view_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:text="Start" /> <Button android:id="@+id/pause_chronometer_view_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:text="Pause" /> <Button android:id="@+id/stop_chronometer_view_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:text="Stop" /> </LinearLayout>
三、在 Activity 中做一個簡單的測試(可以發送簡訊驗證碼的實際應用場景為例)
public class ChronometerActivity extends Activity { private ChronometerView mChronometerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_clock); // 自定義計時器 if (mChronometerView == null) { mChronometerView = (ChronometerView) findViewById(R.id.chronometer_view); mChronometerView.setBaseSeconds(60); mChronometerView.setOnTickChangeListener(new OnTickChangeListener() { @Override public void onTickChanged(ChronometerView view, long curTimeMills) { System.out.println(curTimeMills); view.setEnabled(curTimeMills == 0 || curTimeMills == 60); if (curTimeMills == 0) { mChronometerView.setText("重新發送"); } } }); mChronometerView.setText("點擊發送驗證碼"); } findViewById(R.id.start_chronometer_view_btn).setOnClickListener(mClickListener); findViewById(R.id.pause_chronometer_view_btn).setOnClickListener(mClickListener); findViewById(R.id.stop_chronometer_view_btn).setOnClickListener(mClickListener); } private View.OnClickListener mClickListener = new OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.start_chronometer_view_btn: if (mChronometerView != null) { mChronometerView.start(); } break; case R.id.pause_chronometer_view_btn: if (mChronometerView != null) { mChronometerView.pause(); } break; case R.id.stop_chronometer_view_btn: if (mChronometerView != null) { mChronometerView.stop(); } break; } } }; } 以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。
Android Activity 四種啟動模式深度解析:standard、singleTop、singleTask、singleInstance
原以為 Activity 的啟動模式是個簡單知識點,結果花了一整個下午加晚上才真正弄明白。這篇文章把四種模式的行為、任務棧變化過程以及容易混淆的細節都梳理一遍,歡迎留言討
Android 消息機制深度解析:Handler、Looper、MessageQueue 源碼全通關
接觸 Android 的第一天就聽說了「消息機制」,但那時候的理解僅止於看看別人的博客、記住幾個類名而已,從未真正深入。最近重新過了一遍消息機制和相關源碼,這裡做個系統
Android Activity 啟動模式:singleTop 的堆疊行為
singleTop 的核心規則 singleTop 允許同一個 Activity 有多個實例,但不允許相同 Activity 的實例在棧頂連續堆疊。如果目標 Activi
Android Studio 效率技巧:一次改多行的「多行編輯」
Android Studio 大家應該都不陌生,但多數人大概只用到它最基礎的功能。其實這個 IDE 相當強大,藏著不少實用卻鮮為人知的小技巧,熟練之後能大幅拉高開發效率。