发布于 2017-03-19 10:17:00 | 112 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


各位开发者们在开发中经常会遇到获取短信验证码,获取验证码后需要等待1分钟倒计时,这段时间是不能再次发送短信请求的。这篇文章总结了两种常用的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方式。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定帮助,如果有疑问大家可以留言交流。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务