Android 中倒计时验证两种常用方式实例详解

作者:lqh 时间:2022-08-29 04:44:41 

Android 中倒计时验证两种常用方式实例详解

短信验证码功能,这里总结了两种常用的方式,可以直接拿来使用。看图:

Android 中倒计时验证两种常用方式实例详解

说明:这里的及时从10开始,是为了演示的时间不要等太长而修改的。

1、第一种方式:Timer


/**
* Description:自定义Timer
* <p>
* Created by Mjj on 2016/12/4.
*/

public class TimeCount extends CountDownTimer {

private Button button;

//参数依次为总时长,和计时的时间间隔
 public TimeCount(Button button, long millisInFuture, long countDownInterval) {
   super(millisInFuture, countDownInterval);
   this.button = button;
 }

//计时过程显示
 @Override
 public void onTick(long millisUntilFinished) {
   String time = "(" + millisUntilFinished / 1000 + ")秒";
   setButtonInfo(time, "#c1c1c1", false);
 }

//计时完毕时触发
 @Override
 public void onFinish() {
   setButtonInfo("重新获取", "#f95353", true);
 }

/**
  * 验证按钮在点击前后相关设置
  *
  * @param content 要显示的内容
  * @param color  颜色值
  * @param isClick 是否可点击
  */
 private void setButtonInfo(String content, String color, boolean isClick) {
   button.setText(content);
   button.setBackgroundColor(Color.parseColor(color));
   button.setClickable(isClick);
 }
}

说明:根据自己的需求,在这里修改背景颜色和不同状态显示文字即可,在需要监听的按钮下直接调用new TimerCount(xxx,xxx,xxx).start()即可。

2、第二种方式:Handler


/**
  * 第二种方式:使用Handler
  * <p>
  * 静态内部类:避免内存泄漏
  */
 private static class MyHandler extends Handler {

private final WeakReference<MainActivity> weakReference;

public MyHandler(MainActivity activity) {
     weakReference = new WeakReference<MainActivity>(activity);
   }

@Override
   public void handleMessage(Message msg) {
     super.handleMessage(msg);
     MainActivity activity = weakReference.get();
     if (activity != null) {
       switch (msg.what) {
         case 0:
           if (msg.arg1 == 0) {
             btn2.setText("重新获取");
             btn2.setBackgroundColor(Color.parseColor("#f95353"));
             btn2.setClickable(true);
           } else {
             btn2.setText("(" + msg.arg1 + ")秒");
             btn2.setBackgroundColor(Color.parseColor("#c1c1c1"));
             btn2.setClickable(false);
           }
           break;
       }
     }
   }
 }

/**
  * 监听按钮下直接调用即可
  */
 private void sendMessageClick() {
   new Thread(new Runnable() {
     @Override
     public void run() {
       for (int i = 59; i >= 0; i--) {
         Message msg = myHandler.obtainMessage();
         msg.arg1 = i;
         myHandler.sendMessage(msg);
         try {
           Thread.sleep(1000);
         } catch (InterruptedException e) {
           e.printStackTrace();
         }
       }
     }
   }).start();
 }

说明:此种方式采用的handler实时接收消息来设置Button的状态,对于消息的发送用的是sendMessage方式,也可以使用post方式。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

来源:http://blog.csdn.net/wufeng55/article/details/53457966

标签:Android,倒计时,验证
0
投稿

猜你喜欢

  • Java按照List内存储的对象的某个字段进行排序的实例

    2023-12-11 11:58:35
  • Android ListView 和ScroolView 出现onmeasure空指针的解决办法

    2021-11-25 18:19:03
  • 一文详解Spring的Enablexxx注解使用实例

    2023-09-03 08:43:18
  • C#文件和字节流的转换方法

    2022-03-18 05:39:43
  • java版微信公众平台消息接口应用示例

    2022-10-04 10:22:58
  • Java实现简单的扫雷小程序

    2022-11-18 02:14:07
  • 解析Java的JNI编程中的对象引用与内存泄漏问题

    2023-03-19 20:59:28
  • SpringBoot2.0集成MQTT消息推送功能实现

    2022-02-28 01:55:04
  • Java 滑动窗口最大值的实现

    2021-09-10 15:34:20
  • Java实现窗体程序显示日历

    2022-09-14 11:01:59
  • Hadoop之Mapreduce序列化

    2022-05-08 10:18:19
  • SpringBoot上传临时文件被删除引起报错的解决

    2022-05-28 23:46:24
  • Java发送post方法详解

    2023-10-28 03:55:00
  • springMVC的生命周期详解

    2022-10-29 22:27:40
  • Java堆内存又溢出了!教你一招必杀技(推荐)

    2022-09-22 06:32:55
  • c#委托详解和和示例分享

    2022-10-26 12:29:41
  • Java并发编程ThreadLocalRandom类详解

    2021-07-30 17:56:03
  • JSON 与对象、集合之间的转换的示例

    2021-12-04 20:08:58
  • Java实现简单的飞机大战游戏(控制主飞机篇)

    2023-11-14 13:52:56
  • Java从同步容器到并发容器的操作过程

    2021-10-14 05:26:58
  • asp之家 软件编程 m.aspxhome.com