引导页倒计时(获取验证码)BUTTON

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/yongxiang008/article/details/80198400
public class CountDownButton extends Button {
    private long countDownInterval;
    private long millisInFuture;

    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);
        countDownInterval = typedArray.getInteger(R.styleable.CountDownButton_countDownInterval, 1) * 1000;
        millisInFuture = typedArray.getInteger(R.styleable.CountDownButton_millisInFuture, 5) * 1000;
        typedArray.recycle();
        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.onClick(view);
                }
            }
        });
    }

    public void setCountDownInterval(long countDownInterval) {
        this.countDownInterval = countDownInterval;
    }

    public void setMillisInFuture(long millisInFuture) {
        this.millisInFuture = millisInFuture;
    }

    /**
     * 开启倒计时
     */
    public void start() {
        if (timer == null) {
            timer = new MyCount(millisInFuture, countDownInterval);
        }
        timer.start();
    }

    /**
     * 取消倒计时
     */
    public void cancel() {
        if (timer != null)
            timer.cancel();
    }


    private MyCount timer;

    private class MyCount extends CountDownTimer {

        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            if (mListener != null) {
                mListener.onTick(millisUntilFinished);
            }
        }

        private static final String TAG = "MyCount";

        @Override
        public void onFinish() {
            if (mListener != null) {
                mListener.onFinish();
            }
        }
    }

    private OnCountDownTimeListener mListener;

    public void setonCountDownTimeListener(OnCountDownTimeListener listener) {
        this.mListener = listener;
    }

    /**
     * 倒计时监听接口
     */
    public interface OnCountDownTimeListener {
        /**
         * 按钮监听事件
         *
         * @param view
         */
        void onClick(View view);

        /**
         * CountDownTimer的onTick事件
         *
         * @param millisUntilFinished
         */
        void onTick(long millisUntilFinished);

        /**
         * 倒计时完成事件
         */
        void onFinish();
    }

}

<declare-styleable name="CountDownButton">
    <attr name="millisInFuture" format="integer"/>
    <attr name="countDownInterval" format="integer"/>
</declare-styleable>

展开阅读全文

没有更多推荐了,返回首页