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

驗證碼倒計時按鈕實現:Android賬號注冊常見交互

編輯:關於 Android 程式設計

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

效果預覽

RegisterActivity.java

把倒計時邏輯抽到 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 中呼叫相應的清理方法。

佈局:activity_register.xml

界面結構是「橫向排列 + 分隔線」重複三次,分別對應手機號、驗證碼、密碼三行,最後是注冊按鈕。驗證碼那一行把輸入框與按鈕放在同一個 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,僅改文案是擋不住重複點擊的。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved