温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android中如何实现倒计时验证

发布时间:2021-07-15 11:16:28 来源:亿速云 阅读:85 作者:小新 栏目:移动开发

这篇文章主要介绍了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方式。

感谢你能够认真阅读完这篇文章,希望小编分享的“Android中如何实现倒计时验证”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI