編輯:關於 Android 程式設計
為防止惡意刷短信或郵件,注冊頁面的「獲取驗證碼」按鈕通常都要做倒計時處理:點擊後按鈕變灰並開始倒數,倒數結束才能再次點擊。本文分享一個可複用的封裝思路。

把倒計時邏輯抽到 CountDownButtonHelper 中,Activity 只需在點擊時建立 helper 並啟動,倒數結束後透過 OnFinishListener 把按鈕文案改回「再次獲取」。
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import com.jialianjia.bzw.BaseActivity;
import com.jialianjia.bzw.R;
import com.jialianjia.bzw.utils.CountDownButtonHelper;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
/**
* 注冊功能
* Created by GXS on 2016/4/21.
*/
public class RegisterActivity extends BaseActivity {
private Button btn_yzm;
@ViewInject(R.id.toolbar)
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
ViewUtils.inject(this);
initToolBar(toolbar, "賬號注冊", true);
//獲取驗證碼
btn_yzm = (Button) findViewById(R.id.btn_yzm);
btn_yzm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//參數依次為:按鈕、倒計時提示前綴、總秒數、步長
CountDownButtonHelper helper =
new CountDownButtonHelper(btn_yzm, "倒計時", 60, 1);
helper.setOnFinishListener(new CountDownButtonHelper.OnFinishListener() {
@Override
public void finish() {
btn_yzm.setText("再次獲取");
}
});
helper.start();
}
});
}
}
CountDownButtonHelper 的核心實現思路是:在構造方法中透過 button.post(new Runnable()) 取得按鈕的 Handler,倒數時用 Message 配合 time 字段累減,每次 sendEmptyMessageDelayed 延遲一秒發一次,並在 handleMessage 中刷新按鈕文案與可用狀態,倒數歸零時發送完成通知。用按鈕自身的 Handler 而非新建線程,是為了避免 Activity 銷毀後殘留的倒計時任務引發內存洩漏——記得在 onDestroy 中呼叫相應的清理方法。
界面結構是「橫向排列 + 分隔線」重複三次,分別對應手機號、驗證碼、密碼三行,最後是注冊按鈕。驗證碼那一行把輸入框與按鈕放在同一個 LinearLayout 中,中間用一條 View 做豎向分隔線。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="@style/AppTheme.AppBarOverlay"
android:fitsSystemWindows="true" />
<!-- 手機號 -->
<LinearLayout
android:layout_height="50dp"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:background="@color/white"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_phone"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="@string/tv_info_phone"
android:layout_margin="8dp"
android:textColor="@color/black"
android:layout_gravity="center_vertical"
android:textSize="17sp"/>
<EditText
android:id="@+id/et_phone"
android:layout_width="190dp"
android:layout_height="35dp"
android:background="@null"
android:hint="@string/edt_phone_tips"
android:maxLength="20"
android:layout_gravity="center_vertical"
android:singleLine="true"
android:textSize="16sp" />
<View
android:layout_width="2px"
android:layout_height="50dp"
android:background="@color/driverline" />
<Button
android:id="@+id/btn_yzm"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginLeft="3dp"
android:layout_gravity="center"
android:background="@color/white"
android:text="@string/btn_yzm_text"/>
</LinearLayout>
<View
android:id="@+id/view_line"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/driverline" />
<!-- 驗證碼 -->
<LinearLayout
android:layout_height="50dp"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:background="@color/white"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_yzm"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="@string/tv_yzm_text"
android:layout_margin="8dp"
android:textColor="@color/black"
android:layout_gravity="center_vertical"
android:textSize="17sp"/>
<EditText
android:id="@+id/et_yzm"
android:layout_width="190dp"
android:layout_height="35dp"
android:background="@null"
android:hint="@string/edt_yzm_tips"
android:maxLength="20"
android:layout_gravity="center_vertical"
android:singleLine="true"
android:textSize="16sp" />
</LinearLayout>
<View
android:id="@+id/view_line1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/driverline" />
<!-- 密碼 -->
<LinearLayout
android:layout_height="50dp"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:background="@color/white"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_password"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="@string/tv_password_text"
android:layout_margin="8dp"
android:textColor="@color/black"
android:layout_gravity="center_vertical"
android:textSize="17sp"/>
<EditText
android:id="@+id/et_password"
android:layout_width="190dp"
android:layout_height="35dp"
android:background="@null"
android:hint="@string/edt_password_text"
android:maxLength="20"
android:layout_gravity="center_vertical"
android:singleLine="true"
android:textSize="16sp" />
</LinearLayout>
<View
android:id="@+id/view_line2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/driverline" />
<Button
android:id="@+id/btn_regist"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginTop="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/btn_select"
android:gravity="center"
android:padding="5dp"
android:text="@string/btn_regist_text"
android:textColor="@color/white"
android:textSize="16sp" />
</LinearLayout>
實際接入時還要注意兩點:倒計時只是前端交互,服務端仍必須對驗證碼下發頻率做限流,否則前端攔不住腳本;另外倒計時期間要把按鈕設為 enabled = false,僅改文案是擋不住重複點擊的。
紅米3X怎麼預約購買?紅米3X預約地址與配置介紹
紅米3X預約購買與配置速覽 小米與中國聯通合作推出紅米3X,主打4100mAh大電池與899元售價。目前可在中國聯通官網預約,填寫手機號與所在地區即可。 紅米3X配備5
小豬的 Android 入門之路 Day 8(下):獲取與解析網路資源
本節引言: 在 part 1 中,我們學習了網路互動裡常見的 XML 檔案相關概念,以及在 Android 中解析 XML 的三種不同形式——SAX、DOM、Pull;而
Android 自定義 View 實作:帶數字的進度條 NumberProgressBar
Android 自定義 View:帶數字的進度條 原生 ProgressBar 雖然簡單,但在需要顯示百分比、不同顏色或更細緻視覺效果時,往往不夠用。本文以 Number
Android 自訂系統選單背景圖的進階技巧
Android 系統預設的選單樣式往往比較單調,想要讓選單更有個人風格,我們可以自訂它的背景圖片。本文就來示範如何替系統選單換上自訂背景。 不多說,上圖,見程式碼。 這