button获取验证码 点击倒计时

自定义button

package com.bw.text.logindemo;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.Button;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by wangyaqian on 2016/9/20.
 */
public class TimeButton extends Button
{
    private long lenght = 60 * 1000;// 倒计时长度,默认60    private String textafter = "s后重新获取";
    private String textbefore = "获取验证码";

    private Timer t;
    private TimerTask tt;
    private long time;

    public TimeButton(Context context) {
        super(context);
    }

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

    public void start() {
        initTimer();
        this.setText(time / 1000 + textafter);
        this.setEnabled(false);
        t.schedule(tt, 0, 1000);
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            TimeButton.this.setText(time / 1000 + textafter);
            time -= 1000;
            if (time < 0) {
                TimeButton.this.setEnabled(true);
                TimeButton.this.setText(textbefore);
                clearTimer();
            }
        }
    };

    private void clearTimer() {
        if (tt != null) {
            tt.cancel();
            tt = null;
        }
        if (t != null) {
            t.cancel();
            t = null;
        }
    }

    private void initTimer() {
        time = lenght;
        t = new Timer();
        tt = new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(0x01);
            }
        };
    }

    /**
     * 设置计时时候显示的文本
     */
    public TimeButton setTextAfter(String after)
    {
        this.textafter = after;
        return this;
    }

    /**
     * 设置点击之前的文本
     */
    public TimeButton setTextBefore(String before)
    {
        this.textbefore = before;
        this.setText(textbefore);
        return this;
    }

    /**
     * 设置到计时长度
     * @param lenght 时间 默认毫秒
     * @return
     */
    public TimeButton setLenght(long lenght)
    {
        this.lenght = lenght;
        return this;
    }
}


测试类:

public class OtherActivity extends AppCompatActivity {

    private TimeButton btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other);
        btn = (TimeButton) findViewById(R.id.btn);
        btn.setTextBefore("获取验证码").setTextAfter("s后重新获取").setLenght(60 * 1000);
    }

    public void startDian(View view){
        btn.start();
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值