发布于 2016-02-25 09:54:58 | 228 次阅读 | 评论: 0 | 来源: 网友投递

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

Android移动端操作系统

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


在开发是经常会遇到获取短信验证码,然后获取验证码后需要等待n秒倒计时,这时是不能再次发送短信请求的,这是需要一个倒计时程序,本文给大家分享了实现此功能的代码,需要的朋友参考下

目前越来越多的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:


<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle"
  android:useLevel="false">
  <!-- 背景填充颜色值 -->
  <solid android:color="#6c948b" />
  <!-- radius值越大,越趋于圆形 -->
  <corners android:radius="10dip" />
  <!-- 圆角图像内部填充四周的大小 ,将会以此挤压内部布置的view -->
  <padding
    android:bottom="10dip"
    android:left="10dip"
    android:right="10dip"
    android:top="10dip" />
</shape> 

login_edit_passed_bg.xml:


<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle"
  android:useLevel="false">
  <!-- 背景填充颜色值 -->
  <solid android:color="#509989" />
  <!-- radius值越大,越趋于圆形 -->
  <corners android:radius="10dip" />
  <!-- 圆角图像内部填充四周的大小 ,将会以此挤压内部布置的view -->
  <padding
    android:bottom="10dip"
    android:left="10dip"
    android:right="10dip"
    android:top="10dip" />
</shape>

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



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

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