Android -- 倒计时Button的实现

时间:2021-03-23 22:00:41

1、自定义Button类

public class CountDownButton extends android.support.v7.widget.AppCompatButton implements View.OnClickListener {

private Context mContext;
private OnClickListener mOnClickListener;
private Timer mTimer;//调度器
private TimerTask mTask;
private int temp_duration;
private int countDownTime = 60000;//默认需要倒计时的时间毫秒数
private String countDownText;//倒计时之后的文字显示
private String countDowningFormat ;//正在倒计时时要显示的格式

public CountDownButton(Context context) {
super(context);
init(context, null);

}

public CountDownButton(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}

private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
String text = String.format(countDowningFormat, temp_duration / 1000);
CountDownButton.this.setText(text);
temp_duration -= 1000;
if (temp_duration < 0) { //倒计时结束
CountDownButton.this.setEnabled(true);
CountDownButton.this.setText(countDownText);
stopTimer();
}
}
};

private void init(Context context, AttributeSet attrs) {
this.mContext = context;
countDowningFormat = "重新获取(%d)";
countDownText = "重新获取";
if(null != attrs){
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CountDown);
countDownTime = array.getInt(R.styleable.CountDown_countDownTime, 0);
countDownText = array.getString(R.styleable.CountDown_countDownText);
if(!TextUtils.isEmpty(array.getString(R.styleable.CountDown_countDowningFormat))){
countDowningFormat = array.getString(R.styleable.CountDown_countDowningFormat);
}
array.recycle();
}
setOnClickListener(this);
}

@Override
public void setOnClickListener(OnClickListener onClickListener) {//提供外部访问方法
if (onClickListener instanceof CountDownButton) {
super.setOnClickListener(onClickListener);
} else {
this.mOnClickListener = onClickListener;
}
}

@Override
public void onClick(View view) {
if (mOnClickListener != null) {
mOnClickListener.onClick(view);
}
startTimer();
}

//计时开始
private void startTimer(){
temp_duration = countDownTime;
CountDownButton.this.setEnabled(false);
mTimer = new Timer();
mTask = new TimerTask() {
@Override
public void run() {
mHandler.sendEmptyMessage(0x01);
}
};
mTimer.schedule(mTask, 0, 1000);//调度分配,延迟0秒,时间间隔为1秒
}

//计时结束
private void stopTimer(){
if (mTask != null) {
mTask.cancel();
mTask = null;
}
if (mTimer != null) {
mTimer.cancel();
mTimer = null;
}
}
}
2、Layout使用
<com.term.paimai.helper.CountDownButton
xmlns:countdown="http://schemas.android.com/apk/res/com.term.paimai"
android:id="@+id/get_verify_code"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="@string/get_verify_code"
android:background="@color/white"
android:textColor="@color/colorPrimary"
countdown:countDownTime="10000"
countdown:countDowningFormat="@string/count_down_format"
countdown:countDownText="@string/count_down_text"/>

3、values attrs.xml文件中自定义属性
<?xml version="1.0" encoding="utf-8"?>
<resources>

<declare-styleable name="CountDown">
<attr name="countDownText" format="reference|string" />
<attr name="countDowningFormat" format="reference|string" />
<attr name="countDownTime" format="integer" />
</declare-styleable>

</resources>

参考:

https://www.oschina.net/code/snippet_1187615_49484

http://blog.csdn.net/chengxu_hou/article/details/54972502