手机注册一般都会有一个按钮,默认显示获取验证码,点击之后变成xx秒之后重新获取验证码

在网上查到有两种方法可以实现这种功能,一种是自定义一个timeButton,另外一种是利用封装好的60秒获取验证码工具类

第一种,首先在App.java类中添加一行代码

public static Map<String, Long> map;//用来存放倒计时的时间

然后将TimeButton类加入到工程中,时间默认是60s,也可以随后通过set方法,在代码中修改

package net.zhomi.negotiation.view.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask; import net.zhomi.negotiation.app.App; import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; 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 t;
private TimerTask tt;
private long time;
private Context mContext;
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);
mContext = context;
setOnClickListener(this);
} @SuppressLint("HandlerLeak")
Handler han = 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);
clearTimer();
}
};
}; private void initTimer() {
time = lenght;
t = new Timer();
tt = new TimerTask() { @Override
public void run() {
Log.e("yung", time / 1000 + "");
han.sendEmptyMessage(0x01);
}
};
} private void clearTimer() {
Toast.makeText(mContext, "计时结束", Toast.LENGTH_SHORT).show();
if (tt != null) {
tt.cancel();
tt = null;
}
if (t != null)
t.cancel();
t = 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);
t.schedule(tt, 0, 1000);
// t.scheduleAtFixedRate(task, delay, period);
} /**
* 和activity的onDestroy()方法同步
*/
public void onDestroy() {
if (App.map == null)
App.map = new HashMap<String, Long>();
App.map.put(TIME, time);
App.map.put(CTIME, System.currentTimeMillis());
clearTimer();
Log.e("yung", "onDestroy");
} /**
* 和activity的onCreate()方法同步
*/
public void onCreate(Bundle bundle) {
Log.e("yung", App.map + "");
if (App.map == null)
return;
if (App.map.size() <= 0)// 这里表示没有上次未完成的计时
return;
long time = System.currentTimeMillis() - App.map.get(CTIME)
- App.map.get(TIME);
App.map.clear();
if (time > 0)
return;
else {
initTimer();
this.time = Math.abs(time);
t.schedule(tt, 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;
} /**
* 设置到计时长度
*
* @param lenght
* 时间 默认毫秒
* @return
*/
public TimeButton setLenght(long lenght) {
this.lenght = lenght;
return this;
}
/* *
*/
}

TimeButton在资源文件中的使用与Button并没有太大的不同

<net.zhomi.negotiation.view.utils.TimeButton//TimeButton所在的包
android:id="@+id/btn_getcode"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="15dp"
android:layout_weight="2"
android:background="@null"
android:singleLine="true"
android:text="60秒后重新获取"
android:textColor="@color/balck"
android:textSize="@dimen/normal_text_size" />

第二种

首先看封装的工具类

package com.example.timebtn;

import android.os.CountDownTimer;
import android.widget.Button; public class TimerCount extends CountDownTimer {
private Button bnt; public TimerCount(long millisInFuture, long countDownInterval, Button bnt) {
super(millisInFuture, countDownInterval);
this.bnt = bnt;
} public TimerCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
} @Override
public void onFinish() {
// TODO Auto-generated method stub
bnt.setClickable(true);
bnt.setText("获取验证码");
} @Override
public void onTick(long arg0) {
// TODO Auto-generated method stub
bnt.setClickable(false);
bnt.setText(arg0 / 1000 + "秒后重新获取");
}
}

如何使用:

Button btn = (Button) findViewById(R.id.button1);
TimerCount timer = new TimerCount(60000, 1000, btn);
timer.start();

看起来第二种的代码更简单,但是还是建议用第一种。

最新文章

  1. ELK分析IIS日志
  2. 基于正则的INI读写工具类,支持加密解密
  3. windows,linux,mac生成ssh public key 和 private key
  4. 第20章 priority_queue优先队列容器
  5. matplotlib 绘制柱状图的几个例子
  6. com.velocity.servlet
  7. Rx 入门 示例
  8. C++实现发送HTTP请求
  9. 【转】arm交叉编译器gnueabi、none-eabi、arm-eabi、gnueabihf、gnueabi区别
  10. JQuerry 权威指南的都市笔记
  11. 阿里云CentOS 6.5 设备、执行Docker容器和步骤的方法
  12. redist命令操作(三)--集合Set,有序集合ZSet
  13. Coursera, Deep Learning 5, Sequence Models, week3, Sequence models &amp; Attention mechanism
  14. SSM(Spring+SpringMvc+Mybatis)整合笔记
  15. BZOJ2226:LCMSum(欧拉函数)
  16. springboot(五):spring data jpa的使用
  17. ionic 相关问题解决办法记录
  18. Python常用模块之PIL(手册篇:Image模块)
  19. MySQL学习基础
  20. 借助 Django 的 smart_str 和 smart_unicode 进行编码转换(转)

热门文章

  1. HttpContext是干什么的
  2. python中print的几种用法
  3. spring mybatis 多个数据源配置
  4. Win10 Bash更改默认用户
  5. HDU1584(蜘蛛牌)
  6. Python:包、模块、类、函数的调用
  7. 10-31SQLserver基础--聚合函数、分组
  8. iOS多线程各种安全锁介绍 - 线程同步
  9. java中常用的时间操作
  10. windows下查看端口占用(砖)