這篇文章主要介紹了Andorid實現點擊獲取驗證碼倒計時效果,這種效果大家經常遇到,想知道如何實現的,請閱讀本文
我們在開發中經常用到倒計時的功能,比如發送驗證碼后,倒計時60s再進行驗證碼的獲取,為了方便以后使用,這里做個記錄,講講倒計時器的實現。
1、先進行倒計時工具類的封裝
1 public class CountDownTimerUtils extends CountDownTimer { 2 private TextView mTextView; 3 4 /** 5 * @param textView The TextView 6 * 7 * 8 * @param millisInFuture The number of millis in the future from the call 9 * to {@link #start()} until the countdown is done and {@link #onFinish()} 10 * is called. 11 * @param countDownInterval The interval along the way to receiver 12 * {@link #onTick(long)} callbacks. 13 */ 14 public CountDownTimerUtils(TextView textView, long millisInFuture, long countDownInterval) { 15 super(millisInFuture, countDownInterval); 16 this.mTextView = textView; 17 } 18 19 /** 20 * 倒計時期間會調用 21 * @param millisUntilFinished 22 */ 23 @Override 24 public void onTick(long millisUntilFinished) { 25 mTextView.setClickable(false); //設置不可點擊 26 mTextView.setText(millisUntilFinished / 1000 + "秒"); //設置倒計時時間 27 mTextView.setBackgroundResource(R.drawable.shape_verify_btn_press); //設置按鈕為灰色,這時是不能點擊的 28 29 SpannableString spannableString = new SpannableString(mTextView.getText().toString()); //獲取按鈕上的文字 30 ForegroundColorSpan span = new ForegroundColorSpan(Color.RED); 31 /** 32 * public void setSpan(Object what, int start, int end, int flags) { 33 * 主要是start跟end,start是起始位置,無論中英文,都算一個。 34 * 從0開始計算起。end是結束位置,所以處理的文字,包含開始位置,但不包含結束位置。 35 */ 36 spannableString.setSpan(span, 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);//將倒計時的時間設置為紅色 37 mTextView.setText(spannableString); 38 } 39 40 /** 41 * 倒計時完成后調用 42 */ 43 @Override 44 public void onFinish() { 45 mTextView.setText("重新獲取驗證碼"); 46 mTextView.setClickable(true);//重新獲得點擊 47 mTextView.setBackgroundResource(R.drawable.shape_verify_btn_normal); //還原背景色 48 } 49 }
讓這個工具類繼承CountDownTimer,然后把顯示倒計時的View傳進去,在倒計時期間會調用onTick方法,在這個方法里面對View的倒計時界面進行刷新,倒計時結束后,會調用onFinish方法,在里面恢復View的狀態即可。
2、布局文件
未點擊獲取驗證碼時的view布局 shape_verify_btn_normal.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <shape xmlns:android="http://schemas.android.com/apk/res/android" 3 android:shape="rectangle" > 4 5 <!-- 填充的顏色:這里設置背景透明 --> 6 <solid android:color="@color/maincolor" /> 7 <!-- 邊框的顏色 :不能和窗口背景色一樣--> 8 <stroke 9 android:width="1dp" 10 android:color="@color/white" /> 11 <!-- 設置按鈕的四個角為弧形 --> 12 <!-- android:radius 弧形的半徑 --> 13 <corners android:radius="5dip" /> 14 15 <!-- padding:Button里面的文字與Button邊界的間隔 --> 16 <padding 17 android:bottom="5dp" 18 android:left="5dp" 19 android:right="5dp" 20 android:top="5dp" /> 21 </shape>
點擊獲取驗證碼之后的view布局 shape_verify_btn_press.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <shape xmlns:android="http://schemas.android.com/apk/res/android" 3 android:shape="rectangle" > 4 5 <!-- 填充的顏色:這里設置背景透明 --> 6 <solid android:color="@color/graywhite" /> 7 <!-- 邊框的顏色 :不能和窗口背景色一樣--> 8 <stroke 9 android:width="1dp" 10 android:color="@color/white" /> 11 <!-- 設置按鈕的四個角為弧形 --> 12 <!-- android:radius 弧形的半徑 --> 13 <corners android:radius="5dip" /> 14 15 <!-- padding:Button里面的文字與Button邊界的間隔 --> 16 <padding 17 android:bottom="5dp" 18 android:left="5dp" 19 android:right="5dp" 20 android:top="5dp" /> 21 </shape>
整個界面的布局
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:orientation="vertical" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="@drawable/login_bg"> 6 7 <EditText 8 android:id="@+id/rst_phone_number" 9 android:layout_width="match_parent" 10 android:layout_height="40dp" 11 android:inputType="phone" 12 android:hint="@string/phone_number" 13 android:textSize="16sp" 14 android:textColor="@color/black" 15 android:singleLine="true" 16 android:background="@drawable/shape_form" 17 android:textCursorDrawable="@drawable/text_cursor" 18 android:layout_marginLeft="30dp" 19 android:layout_marginRight="30dp" 20 android:layout_marginTop="30dp" 21 android:layout_gravity="center_vertical"/> 22 23 <LinearLayout 24 android:layout_width="match_parent" 25 android:layout_height="40dp" 26 android:layout_marginLeft="30dp" 27 android:layout_marginRight="30dp" 28 android:layout_marginTop="15dp" 29 android:orientation="horizontal"> 30 31 <EditText 32 android:id="@+id/rst_verify_code" 33 android:layout_width="wrap_content" 34 android:layout_height="match_parent" 35 android:layout_weight="1" 36 android:inputType="number" 37 android:hint="@string/rst_verify_code" 38 android:textSize="16sp" 39 android:textColor="@color/black" 40 android:singleLine="true" 41 android:background="@drawable/shape_form" 42 android:textCursorDrawable="@drawable/text_cursor" /> 43 44 <TextView 45 android:id="@+id/rst_send_code" 46 android:layout_width="130dp" 47 android:layout_height="match_parent" 48 android:text="@string/rst_send_code" 49 android:textSize="13sp" 50 android:textColor="@color/white" 51 android:gravity="center" 52 android:layout_marginLeft="10dp" 53 android:background="@drawable/shape_verify_btn_normal"/> 54 55 </LinearLayout> 56 57 <Button 58 android:id="@+id/rst_next_step" 59 android:layout_width="match_parent" 60 android:layout_height="40dp" 61 android:layout_margin="30dp" 62 android:text="@string/rst_next_step" 63 android:textSize="15sp" 64 android:textColor="@color/white" 65 android:background="@drawable/shape_btn"/> 66 67 </LinearLayout>
這里布局有個問題,因為獲取驗證碼這個TextView中的字會在倒計時期間有變化,而且每次字的變化長度不一樣,如果把它的layout_width設為wrap_content,那么這個TextView的長度會變化,影響界面美觀,所以可以把它的長度固定,然后把驗證碼輸入框的layout_weight設為1,這樣就可以了。
3、具體使用方法
1 // 發送成功進入倒計時 2 countDownTimer = new CountDownTimerUtils(tv_send_code, 60000, 1000); 3 countDownTimer.start();
其中60000代表要倒計時的時長,即60s;1000代表每次遞減1s。
以上就是具體的實現過程,下面附幾張效果圖!
以上就是本文的全部內容,希望對大家的學習有所幫助。