在一些應(yīng)用的用戶登錄,或者注冊時候,會通過手機,和動態(tài)驗證碼的方式進行驗證,并且為了減少對服務(wù)器發(fā)送信息的請求,會設(shè)置一個時間段來讓用戶等待驗證碼,并且允許意外輸入錯誤之后的修改,如下圖:



Android有一個類CountDownTimer專門處理這個問題,并且包含用于處理按鈕的回調(diào),實現(xiàn)如下:
btn_get_authCode.setOnClickListener(new android.view.View.OnClickListener() {
? ? ? ?public void onClick(View v) {
? ? ? ? ? ? ?new CountDownTimer(60000, 1000) {? ? ? ? ? ? ? ? ? ? ?public void onTick(long millisUntilFinished() {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? btn_get_authCode.setText(millisUntilFinished / 1000 + "秒后重新發(fā)送");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? btn_get_authCode.setEnable(false);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? public void onFinish() {
? ? ? ? ? ? ? ? ? ? ? ? ?btn_get_authCode.setText("重新獲取驗證碼");
? ? ? ? ? ? ? ? ? ? ? ? ?btn_get_authCode.setEnable(true);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}.start();
? ? ? ? ? ? ? ?}
? ? ? ?});