button获取验证码 点击倒计时
来源:互联网 发布:淘宝美女模特 编辑:程序博客网 时间:2024/05/16 18:52
自定义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
- button获取验证码 点击倒计时
- Android 点击Button获取验证码倒计时效果
- 获取验证码button,倒计时按钮,继承Button
- 点击获取验证码显示倒计时
- 点击按钮获取验证码,倒计时。
- android 点击获取验证码显示倒计时并不可用
- 获取验证码按钮,点击后倒计时功能的实现
- 获取验证码倒计时
- 获取验证码倒计时
- 获取验证码倒计时
- 倒计时获取验证码
- 获取验证码倒计时
- 获取验证码倒计时
- 获取验证码倒计时
- 获取验证码倒计时
- 获取验证码倒计时
- 获取验证码倒计时
- Button的点击倒计时
- find命令的使用技巧
- IONIC多条聊天记渲染完毕后滚动到底部解决方案
- hdu 2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活
- 如何跳到系统设置里的各种设置界面
- iOS循环引用问题
- button获取验证码 点击倒计时
- 彻底理解PHP的SESSION机制
- 大数据可视化的最新动态
- NewtonSoft.JSON.dll LitJson.dll
- 百度霸屏截流量抢粉丝技术 没点儿行动力千万别点击
- 函数
- arm程序的反汇编程序 试读
- iOS 中的加密方式
- 15章上机练习1