栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android开发之获取短信验证码后按钮背景变化并且出现倒计时

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android开发之获取短信验证码后按钮背景变化并且出现倒计时

目前越来越多的app在注册或是进行对应操作时,要求获取短信验证码,在点击了获取短信验证码的按钮后,就是出现倒计时,比如倒计时120S,在倒计时期间内,按钮背景变化并且出现倒计时,当倒计时结束后,如果你没有获取到验证码,可以再次点击。

代码如下所示:

VerCodeTimer mVerCodeTimer=(Button) findViewById(R.id.login_get_ver_code);
private class VerCodeTimer extends CountDownTimer {
    private int seconds;
    private int interval;
    //millisInFuture为你设置的此次倒计时的总时长,比如60秒就设置为60000
    //countDownInterval为你设置的时间间隔,比如一般为1秒,根据需要自定义。
    public VerCodeTimer(long millisInFuture, long countDownInterval) {
      super(millisInFuture, countDownInterval);
      seconds = (int) (millisInFuture / 1000);
      interval= (int) (countDownInterval/1000);
    }
    //每过你规定的时间间隔做的操作
    @Override
    public void onTick(long millisUntilFinished) {
      getVerCodeButton.setText((seconds-interval) + "秒后重新获取");
    }
    //倒计时结束时做的操作↓↓
    @Override
    public void onFinish() {
      getVerCodeButton.setTextSize(10);
      getVerCodeButton.setText("重新获取验证码");
      getVerCodeButton.setClickable(true);
      getVerCodeButton.setBackgroundResource(R.drawable.login_get_ver_code_before_bg);
    }
  }
  
 @Override
  public void onBackPressed() {
    if (mVerCodeTimer != null)
      mVerCodeTimer.cancel();
    super.onBackPressed();
  } 

使用的时候:

getVerCodeButton.setTextSize(11);
 getVerCodeButton.setClickable(false);
 getVerCodeButton.setBackgroundResource(R.drawable.login_get_ver_code_ago_bg);
 mVerCodeTimer = new VerCodeTimer(60000, 1000);
 mVerCodeTimer.start(); 

login_edit_normal_bg.xml:



  
  
  
  
  
  
 

login_edit_passed_bg.xml:



  
  
  
  
  
  

以上所述是小编给大家介绍了Android开发之获取短信验证码后按钮背景变化并且出现倒计时 的全部代码,希望本段代码能够帮助大家。同时感谢大家一直以来对考高分网网站的支持。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/163626.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号