我們在開發中經常用到倒計時的功能,比如發送驗證碼後,倒計時60s再進行驗證碼的擷取,為了友善以後使用,這裡做個記錄,講講倒計時器的實作。
1、先進行倒計時工具類的封裝
public class CountDownTimerUtils extends CountDownTimer {
private TextView mTextView;
/**
* @param textView The TextView
*
*
* @param millisInFuture The number of millis in the future from the call
* to {@link #start()} until the countdown is done and {@link #onFinish()}
* is called.
* @param countDownInterval The interval along the way to receiver
* {@link #onTick(long)} callbacks.
*/
public CountDownTimerUtils(TextView textView, long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
this.mTextView = textView;
}
/**
* 倒計時期間會調用
* @param millisUntilFinished
*/
@Override
public void onTick(long millisUntilFinished) {
mTextView.setClickable(false); //設定不可點選
mTextView.setText(millisUntilFinished / 1000 + "秒"); //設定倒計時時間
mTextView.setBackgroundResource(R.drawable.shape_verify_btn_press); //設定按鈕為灰色,這時是不能點選的
SpannableString spannableString = new SpannableString(mTextView.getText().toString()); //擷取按鈕上的文字
ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
/**
* public void setSpan(Object what, int start, int end, int flags) {
* 主要是start跟end,start是起始位置,無論中英文,都算一個。
* 從0開始計算起。end是結束位置,是以處理的文字,包含開始位置,但不包含結束位置。
*/
spannableString.setSpan(span, 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);//将倒計時的時間設定為紅色
mTextView.setText(spannableString);
}
/**
* 倒計時完成後調用
*/
@Override
public void onFinish() {
mTextView.setText("重新擷取驗證碼");
mTextView.setClickable(true);//重新獲得點選
mTextView.setBackgroundResource(R.drawable.shape_verify_btn_normal); //還原背景色
}
}
讓這個工具類繼承CountDownTimer,然後把顯示倒計時的View傳進去,在倒計時期間會調用onTick方法,在這個方法裡面對View的倒計時界面進行重新整理,倒計時結束後,會調用onFinish方法,在裡面恢複View的狀态即可。
2、布局檔案
未點選擷取驗證碼時的view布局 shape_verify_btn_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 填充的顔色:這裡設定背景透明 -->
<solid android:color="@color/maincolor" />
<!-- 邊框的顔色 :不能和視窗背景色一樣-->
<stroke
android:width="1dp"
android:color="@color/white" />
<!-- 設定按鈕的四個角為弧形 -->
<!-- android:radius 弧形的半徑 -->
<corners android:radius="5dip" />
<!-- padding:Button裡面的文字與Button邊界的間隔 -->
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
</shape>
點選擷取驗證碼之後的view布局 shape_verify_btn_press.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 填充的顔色:這裡設定背景透明 -->
<solid android:color="@color/graywhite" />
<!-- 邊框的顔色 :不能和視窗背景色一樣-->
<stroke
android:width="1dp"
android:color="@color/white" />
<!-- 設定按鈕的四個角為弧形 -->
<!-- android:radius 弧形的半徑 -->
<corners android:radius="5dip" />
<!-- padding:Button裡面的文字與Button邊界的間隔 -->
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
</shape>
整個界面的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_bg">
<EditText
android:id="@+id/rst_phone_number"
android:layout_width="match_parent"
android:layout_height="40dp"
android:inputType="phone"
android:hint="@string/phone_number"
android:textSize="16sp"
android:textColor="@color/black"
android:singleLine="true"
android:background="@drawable/shape_form"
android:textCursorDrawable="@drawable/text_cursor"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="30dp"
android:layout_gravity="center_vertical"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="15dp"
android:orientation="horizontal">
<EditText
android:id="@+id/rst_verify_code"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:inputType="number"
android:hint="@string/rst_verify_code"
android:textSize="16sp"
android:textColor="@color/black"
android:singleLine="true"
android:background="@drawable/shape_form"
android:textCursorDrawable="@drawable/text_cursor" />
<TextView
android:id="@+id/rst_send_code"
android:layout_width="130dp"
android:layout_height="match_parent"
android:text="@string/rst_send_code"
android:textSize="13sp"
android:textColor="@color/white"
android:gravity="center"
android:layout_marginLeft="10dp"
android:background="@drawable/shape_verify_btn_normal"/>
</LinearLayout>
<Button
android:id="@+id/rst_next_step"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="30dp"
android:text="@string/rst_next_step"
android:textSize="15sp"
android:textColor="@color/white"
android:background="@drawable/shape_btn"/>
</LinearLayout>
這裡布局有個問題,因為擷取驗證碼這個TextView中的字會在倒計時期間有變化,而且每次字的變化長度不一樣,如果把它的layout_width設為wrap_content,那麼這個TextView的長度會變化,影響界面美觀,是以可以把它的長度固定,然後把驗證碼輸入框的layout_weight設為1,這樣就可以了。
3、具體使用方法
// 發送成功進入倒計時
countDownTimer = new CountDownTimerUtils(tv_send_code, 60000, 1000);
countDownTimer.start();
其中60000代表要倒計時的時長,即60s;1000代表每次遞減1s。
以上就是具體的實作過程,下面附幾張效果圖!
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIyVGduV2QvwVe0lmdhJ3ZvwFM38CXlZHbvN3cpR2Lc1TPB10QGtWUCpEMJ9CXsxWam9CXwADNvwVZ6l2c052bm9CXUJDT1wkNhVzLcRnbvZ2LcZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39TOwMDN1ETM0EjNwgDM2EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)