Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于CountDownButton倒计时控件的一些问题 #22

Closed
cllstudy opened this issue May 13, 2019 · 2 comments
Closed

关于CountDownButton倒计时控件的一些问题 #22

cllstudy opened this issue May 13, 2019 · 2 comments

Comments

@cllstudy
Copy link

问题描述:
在布局文件中使用CountDownButton控件,在代码中设置改控件的监听,需求是这样的,点击CountDownButton弹出一个底部对话框,此时不进行倒计时,弹出对话框后需要输入图形验证码上的数字,验证通过后开始倒计时.我试过setEnableCountDown方法都无济于事,而startCountDown()方法开启倒计时确是私有的,请问我该如何实现呢

@cllstudy cllstudy changed the title 关于CountDownButton倒计时空间的一些问题 关于CountDownButton倒计时控件的一些问题 May 13, 2019
@cllstudy
Copy link
Author

自己重写了一个,建议CountDownButton开放startCountDown方法

package com.wsy.companypassword.view;

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.AppCompatButton;
import android.util.AttributeSet;
/**
* @author cll
* @Time 2019/5/13 16:12
* @Description 倒计时button
*/
public class CountDownButton extends AppCompatButton {

    private long totalTime = 60 * 1000;// 默认60秒
    private String label = "s";
    private long time;
    private long step = 1000;
    private int interval = 1000;

    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            time -= step;
            if (time <= 0) {
                reset();
            }
            else {
                refreshText();
                mHandler.sendEmptyMessageDelayed(0, step);
            }
        }
    };

    public CountDownButton(Context context) {
        this(context, null);
    }

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

    private void init(Context context, AttributeSet attrs) {
        setHintTextColor(getTextColors());
    }

    public void start() {
        this.setEnabled(false);
        this.time = totalTime;
        refreshText();
        mHandler.sendEmptyMessageDelayed(0, step);
    }

    private void refreshText() {
        long t = (time / interval);
        if (t > 0) {
            this.setText(t + label);
        }
    }

    public void reset() {
        setText("获取验证码");
        setEnabled(true);
    }

    /**
     * Set count down total time
     * <p>
     * <code> setTotalTime(30000);// 30s</code>
     * </p>
     *
     * @param totalTime
     *
     * @return CountDownButton self
     */
    public CountDownButton setTotalTime(long totalTime) {
        this.totalTime = totalTime;
        return this;
    }

    /**
     * Set count down step
     * <p>
     * <code> setStep(1000);// 1s</code>
     * </p>
     *
     * @param step count down step, micro seconds
     *
     * @return CountDownButton self
     */
    public CountDownButton setStep(long step) {
        if (step > 0) {
            this.step = step;
        }
        return this;
    }

    /**
     * Set count down text refresh interval.
     * <p>
     * <code> setInterval(1000);//1s, text display: (getRemainingTime() / interval) + label</code>
     * </p>
     *
     * @param interval count down text refresh interval, micro seconds
     *
     * @return CountDownButton self
     */
    public CountDownButton setInterval(int interval) {
        if (interval > 0) {
            this.interval = interval;
        }
        return this;
    }

    public long getRemainingTime() {
        return time;
    }

    public long getStep() {
        return step;
    }
}

@xuexiangjys
Copy link
Owner

看你的描述,我建议你使用CountDownButtonHelper,这样更灵活些

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants