倒计时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"?>
<resources>
    <declare-styleable name="CountDownButton">
        <attr name="millisInFuture" format="integer"/>
        <attr name="countDownInterval" format="integer"/>
    </declare-styleable>
</resources>

布局

<com.nevermore.rounrect.CountDownButton
        android:layout_centerInParent="true"
        android:id="@+id/cdb"
        android:background="@drawable/bg_count_down"
        android:text="发送验证码"
        android:gravity="center"
        app:millisInFuture="20"
        app:countDownInterval="1"
        android:layout_width="110dp"
        android:padding="15dp"
        android:layout_height="wrap_content" />

bg_count_down

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true">
        <shape android:shape="rectangle">
            <corners android:radius="8dp"/>
            <solid android:color="#2eb7ed"/>
        </shape>
    </item>
    <item android:state_enabled="false">
        <shape android:shape="rectangle">
            <corners android:radius="8dp"/>
            <solid android:color="#999999"/>
        </shape>
    </item>
</selector>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值