CountDownTimer
項目中經常用到倒計時的功能,比如說限時搶購,手機獲取驗證碼等等。而google官方也幫我們封裝好了一個類:CountDownTimer,使我們的開發更加方便。
CountDownTimer是一個抽象類,有兩個抽象方法,它的API很簡單
public abstract void onTick(long millisUntilFinished);//這個是每次間隔指定時間的回調,millisUntilFinished:剩餘的時間,單位毫秒
public abstract void onFinish();//這個是倒計時結束的回調
獲取驗證碼按鈕佈局
<TextView
android:id="@+id/tv_get_verifycode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="50dp"
android:background="@drawable/selector_get_verify_code_bg"
android:enabled="true"
android:gravity="center"
android:padding="8dp"
android:text="獲取驗證碼"
android:textColor="@color/selector_color_get_verify_code"
android:textSize="14sp" />
selector_color_get_verify_code.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#ffffff" android:state_enabled="true"/>
<item android:color="#333333" android:state_enabled="false"/>
</selector>
shape_get_verify_code_enable.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ff4415" />
<corners android:radius="10dp" />
</shape>
shape_get_verify_no_enable.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#d9d9d9" />
<corners android:radius="10dp" />
</shape>
selector_get_verify_code_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/shape_get_verify_code_enable" android:state_enabled="true"/>
<item android:drawable="@drawable/shape_get_verify_no_enable" android:state_enabled="false"/>
</selector>
CountDownTimeUtil
/**
* Cerated by xiaoyehai
* Create date : 2019/11/11 19:16
* description :
*/
public class CountDownTimeUtil {
private WeakReference<TextView> tvCodeWr;//控件軟引用,防止內存泄漏
private CountDownTimer timer;
public CountDownTimeUtil(TextView tvCode) {
super();
this.tvCodeWr = new WeakReference(tvCode);
}
public void runTimer() {
//倒計時的總時長 每次的間隔時間 單位都是毫秒
timer = new CountDownTimer(60 * 1000 - 1, 1000) {
/**
* 這個是倒計時結束的回調
*/
@Override
public void onFinish() {
if (tvCodeWr.get() != null) {
tvCodeWr.get().setClickable(true);
tvCodeWr.get().setEnabled(true);
tvCodeWr.get().setText("獲取驗證碼");
}
cancel();
}
/**
* 這個是每次間隔指定時間的回調
*
* @param millisUntilFinished 剩餘的時間,單位毫秒
*/
@Override
public void onTick(long millisUntilFinished) {
if (tvCodeWr.get() != null) {
tvCodeWr.get().setClickable(false);
tvCodeWr.get().setEnabled(false);
tvCodeWr.get().setText(millisUntilFinished / 1000 + "s後重新獲取");
}
}
}.start();
}
/**
* 這個方法可以在activity或者fragment銷燬的時候調用,防止內存泄漏
* 如果在activity或者fragment關閉銷燬的時候沒有調用cancle方法,它的onTick方法還是會繼續執行,這個時候UI控件都爲空,不注意判斷的話很容易空指針
*/
public void cancel() {
if (timer != null) {
timer.onFinish();
timer.cancel();
timer = null;
}
}
}
在Activity中使用
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
mTvGetVerifyCode = (TextView) findViewById(R.id.tv_get_verifycode);
mTvGetVerifyCode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCountDownTimeUtil = new CountDownTimeUtil(mTvGetVerifyCode);
mCountDownTimeUtil.runTimer();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mCountDownTimeUtil != null) {
mCountDownTimeUtil.cancel();
}
}