Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

簡訊驗證碼倒數計時實作:按鈕狀態切換與背景樣式變化

編輯:關於 Android 開發

簡訊驗證碼倒數計時實作:按鈕狀態切換與背景樣式變化

「發送驗證碼」按鈕點下去之後要進入 30 秒冷卻,期間變灰不可點並顯示剩餘秒數,倒數結束後恢復可點——這是註冊頁幾乎必有的一個互動。實作上要注意的是倒數計時不能寫在主執行緒,否則會卡住畫面。

做法是開一條背景執行緒跑秒數,再用 post() 把 UI 更新丟回主執行緒:

private Button getVerCodeButton;

// 初始化
getVerCodeButton = (Button) findViewById(R.id.login_get_ver_code);

/** 顯示倒數計時 */
public void showTime() {
    new Thread(new Runnable() {
        boolean result = true;
        int time = 30;

        @Override
        public void run() {
            while (result) {
                time--;
                SystemClock.sleep(1000);

                getVerCodeButton.post(new Runnable() {
                    @Override
                    public void run() {
                        getVerCodeButton.setTextSize(11);
                        getVerCodeButton.setText(time + "秒後重新獲取");
                        getVerCodeButton.setClickable(false);
                        getVerCodeButton.setBackgroundResource(
                                R.drawable.login_get_ver_code_ago_bg);
                    }
                });

                if (time <= 1) {
                    result = false;
                    getVerCodeButton.post(new Runnable() {
                        @Override
                        public void run() {
                            getVerCodeButton.setTextSize(10);
                            getVerCodeButton.setText("重新獲取驗證碼");
                            getVerCodeButton.setClickable(true);
                            getVerCodeButton.setBackgroundResource(
                                    R.drawable.login_get_ver_code_before_bg);
                        }
                    });
                }
            }
        }
    }).start();
}

兩個狀態各有一張背景圖,本質都是圓角矩形,只用填色深淺區分「可點」與「不可點」。

login_edit_normal_bg.xml(可點擊狀態)

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    android:useLevel="false">

    <!-- 背景填充顏色 -->
    <solid android:color="#6c948b" />

    <!-- radius 越大越趨近圓形 -->
    <corners android:radius="10dip" />

    <!-- 內距,會挤压內部的 view -->
    <padding
        android:bottom="10dip"
        android:left="5dip"
        android:right="10dip"
        android:top="10dip" />
</shape>

login_edit_passed_bg.xml(冷卻中狀態)

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    android:useLevel="false">

    <solid android:color="#509989" />
    <corners android:radius="10dip" />
    <padding
        android:bottom="10dip"
        android:left="5dip"
        android:right="10dip"
        android:top="10dip" />
</shape>

這份實作有幾個地方在正式專案中值得改進:

  • 用 Handler 取代裸 Thread:Android 更推薦用 Handler + Message 或 CountDownTimer,後者本身就是為倒數場景設計的,寫起來短很多。
  • 記得在 onDestroy 中停止計時:否則使用者離開畫面後,那條執行緒還在跑並嘗試更新已經銷毀的 View,容易出問題。
  • 用 setEnabled(false) 而非 setClickable(false):兩者都能阻止點擊,但 setEnabled 會自動觸發 state_enabled 相關的樣式切換,語意也更準確。
  • 冷卻時間最好由後端回傳:若發送失敗或已有冷卻中,前端的 30 秒可能與實際狀況不符。

一個更簡潔的 CountDownTimer 寫法可以長這樣:

new CountDownTimer(30 * 1000, 1000) {
    @Override
    public void onTick(long millisUntilFinished) {
        getVerCodeButton.setText(millisUntilFinished / 1000 + "秒後重新獲取");
        getVerCodeButton.setEnabled(false);
    }

    @Override
    public void onFinish() {
        getVerCodeButton.setText("重新獲取驗證碼");
        getVerCodeButton.setEnabled(true);
    }
}.start();
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved