android倒计时获取验证码按钮

来源:互联网 发布:python重定向到文件 编辑:程序博客网 时间:2024/06/09 22:47

Android倒计时获取验证码按钮,先上图:



封装类:

package KiraUtils.view;import android.annotation.SuppressLint;import android.content.Context;import android.graphics.Color;import android.os.Bundle;import android.os.Handler;import android.util.AttributeSet;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import java.util.HashMap;import java.util.Map;import java.util.Timer;import java.util.TimerTask;/** * Created by kira on 2017/4/24. */public class TimeButton extends Button implements OnClickListener {    private long lenght = 60 * 1000;// 倒计时长度,默认60秒    private String textafter = "秒后重新获取";    private String textbefore = "获取验证码";    private final String TIME = "time";    private final String CTIME = "ctime";    private OnClickListener mOnclickListener;    private Timer timer;    private TimerTask timerTask;    private long time;    Map<String, Long> map = new HashMap<String, Long>();    public TimeButton(Context context) {        super(context);        setOnClickListener(this);    }    public TimeButton(Context context, AttributeSet attrs) {        super(context, attrs);        setOnClickListener(this);    }    //实现倒计时    @SuppressLint("HandlerLeak")    Handler handler = new Handler() {        public void handleMessage(android.os.Message msg) {            TimeButton.this.setText(time / 1000 + textafter);            time -= 1000;            if (time < 0) {                TimeButton.this.setEnabled(true);                TimeButton.this.setText(textbefore);                TimeButton.this.setTextColor(Color.parseColor("#008040"));                clearTimer();            }        }    };    private void initTimer() {        time = lenght;        timer = new Timer();        timerTask = new TimerTask() {            @Override            public void run() {                handler.sendEmptyMessage(0x01);            }        };    }    private void clearTimer() {        if (timerTask != null) {            timerTask.cancel();            timerTask = null;        }        if (timerTask != null)            timerTask.cancel();        timerTask = null;    }    @Override    public void setOnClickListener(OnClickListener l) {        if (l instanceof TimeButton) {            super.setOnClickListener(l);        } else            this.mOnclickListener = l;    }    @Override    public void onClick(View v) {        if (mOnclickListener != null)            mOnclickListener.onClick(v);        initTimer();        this.setText(time / 1000 + textafter);        this.setEnabled(false);        timer.schedule(timerTask, 0, 1000);    }    public void onDestroy() {        if (map == null)            map = new HashMap<String, Long>();        map.put(TIME, time);        map.put(CTIME, System.currentTimeMillis());        clearTimer();//activity销毁时同时将timerTask取消掉    }    public void onCreate(Bundle bundle) {        if (map == null)            return;        if (map.size() <= 0)// 上次未完成的计时            return;        long time = System.currentTimeMillis() - map.get(CTIME)                -map.get(TIME);        map.clear();        if (time > 0)            return;        else {            initTimer();            this.time = Math.abs(time);            timer.schedule(timerTask, 0, 1000);            this.setText(time + textafter);            this.setEnabled(false);        }    }    //设置计时显示文本    public TimeButton setTextAfter(String text1) {        this.textafter = text1;        return this;    }    //设置点击之前文本    public TimeButton setTextBefore(String text0) {        this.textbefore = text0;        this.setText(textbefore);        return this;    }    //设置倒计时时间    public TimeButton setLenght(long lenght) {        this.lenght = lenght;        return this;    }}


布局文件直接引用类就行了

<KiraUtils.view.TimeButton    android:id="@+id/tv_getcode"    android:layout_width="wrap_content"    android:layout_height="match_parent"    android:layout_gravity="center_vertical"    android:text="获取验证码"    android:background="@null"    android:gravity="center"    android:textColor="#008040" />

在Activity中为按钮添加点击事件就行了

<KiraUtils.view.TimeButton    android:id="@+id/tv_getcode"    android:layout_width="wrap_content"    android:layout_height="match_parent"    android:layout_gravity="center_vertical"    android:text="获取验证码"    android:background="@null"    android:gravity="center"    android:textColor="#008040" />



原创粉丝点击