倒计时button(借鉴CountDownButton)
看了简书上的一片关于CountDownButton的文章:Hyena-CountDownButton-倒计时按钮
用了里面的思路自己写了下。
主要代码
public class CountDownButton extends Button {
private String text;
private String content;
public CountDownButton(Context context) {
this(context, null);
}
public CountDownButton(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CountDownButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
TypedArray typedArray = context.getResources().obtainAttributes(attrs, R.styleable.CountDownButton);
long countDownInterval = typedArray.getInteger(R.styleable.CountDownButton_countDownInterval, 1)*1000;
long millisInFuture = typedArray.getInteger(R.styleable.CountDownButton_millisInFuture, 5)*1000;
timer = new MyCount(millisInFuture, countDownInterval);
typedArray.recycle();
}
public void start() {
content = getText().toString().trim();
timer.start();
this.setEnabled(false);
}
public void cancel() {
timer.cancel();
}
private MyCount timer;
private class MyCount extends CountDownTimer {
/**
* @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 receive
* {@link #onTick(long)} callbacks.
*/
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
String time = "" + (millisUntilFinished / 1000);
Log.i(TAG, "onTick: "+time);
CountDownButton.this.setText(time);
}
private static final String TAG = "MyCount";
@Override
public void onFinish() {
CountDownButton.this.setText(content);
CountDownButton.this.setEnabled(true);
}
}
}
自定义属性
<?xml version="1.0" encoding="utf-8"?>
布局
bg_count_down
<?xml version="1.0" encoding="utf-8"?>
-
-