Android实现点击获取验证码倒计时效果
作者:yypccc 发布时间:2022-08-29 09:23:41
标签:android,验证码,倒计时
我们在开发中经常用到倒计时的功能,比如发送验证码后,倒计时60s再进行验证码的获取,为了方便以后使用,这里做个记录,讲讲倒计时器的实现。
1、先进行倒计时工具类的封装
public class CountDownTimerUtils extends CountDownTimer {
private TextView mTextView;
/**
* @param textView The TextView
*
*
* @param millisInFuture The number of millis in the future from the call
* to {@link #start()} until the countdown is done and {@link #onFinish()}
* is called.
* @param countDownInterval The interval along the way to receiver
* {@link #onTick(long)} callbacks.
*/
public CountDownTimerUtils(TextView textView, long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
this.mTextView = textView;
}
/**
* 倒计时期间会调用
* @param millisUntilFinished
*/
@Override
public void onTick(long millisUntilFinished) {
mTextView.setClickable(false); //设置不可点击
mTextView.setText(millisUntilFinished / 1000 + "秒"); //设置倒计时时间
mTextView.setBackgroundResource(R.drawable.shape_verify_btn_press); //设置按钮为灰色,这时是不能点击的
SpannableString spannableString = new SpannableString(mTextView.getText().toString()); //获取按钮上的文字
ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
/**
* public void setSpan(Object what, int start, int end, int flags) {
* 主要是start跟end,start是起始位置,无论中英文,都算一个。
* 从0开始计算起。end是结束位置,所以处理的文字,包含开始位置,但不包含结束位置。
*/
spannableString.setSpan(span, 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);//将倒计时的时间设置为红色
mTextView.setText(spannableString);
}
/**
* 倒计时完成后调用
*/
@Override
public void onFinish() {
mTextView.setText("重新获取验证码");
mTextView.setClickable(true);//重新获得点击
mTextView.setBackgroundResource(R.drawable.shape_verify_btn_normal); //还原背景色
}
}
让这个工具类继承CountDownTimer,然后把显示倒计时的View传进去,在倒计时期间会调用onTick方法,在这个方法里面对View的倒计时界面进行刷新,倒计时结束后,会调用onFinish方法,在里面恢复View的状态即可。
2、布局文件
未点击获取验证码时的view布局 shape_verify_btn_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 填充的颜色:这里设置背景透明 -->
<solid android:color="@color/maincolor" />
<!-- 边框的颜色 :不能和窗口背景色一样-->
<stroke
android:width="1dp"
android:color="@color/white" />
<!-- 设置按钮的四个角为弧形 -->
<!-- android:radius 弧形的半径 -->
<corners android:radius="5dip" />
<!-- padding:Button里面的文字与Button边界的间隔 -->
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
</shape>
点击获取验证码之后的view布局 shape_verify_btn_press.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 填充的颜色:这里设置背景透明 -->
<solid android:color="@color/graywhite" />
<!-- 边框的颜色 :不能和窗口背景色一样-->
<stroke
android:width="1dp"
android:color="@color/white" />
<!-- 设置按钮的四个角为弧形 -->
<!-- android:radius 弧形的半径 -->
<corners android:radius="5dip" />
<!-- padding:Button里面的文字与Button边界的间隔 -->
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
</shape>
整个界面的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_bg">
<EditText
android:id="@+id/rst_phone_number"
android:layout_width="match_parent"
android:layout_height="40dp"
android:inputType="phone"
android:hint="@string/phone_number"
android:textSize="16sp"
android:textColor="@color/black"
android:singleLine="true"
android:background="@drawable/shape_form"
android:textCursorDrawable="@drawable/text_cursor"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="30dp"
android:layout_gravity="center_vertical"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="15dp"
android:orientation="horizontal">
<EditText
android:id="@+id/rst_verify_code"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:inputType="number"
android:hint="@string/rst_verify_code"
android:textSize="16sp"
android:textColor="@color/black"
android:singleLine="true"
android:background="@drawable/shape_form"
android:textCursorDrawable="@drawable/text_cursor" />
<TextView
android:id="@+id/rst_send_code"
android:layout_width="130dp"
android:layout_height="match_parent"
android:text="@string/rst_send_code"
android:textSize="13sp"
android:textColor="@color/white"
android:gravity="center"
android:layout_marginLeft="10dp"
android:background="@drawable/shape_verify_btn_normal"/>
</LinearLayout>
<Button
android:id="@+id/rst_next_step"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="30dp"
android:text="@string/rst_next_step"
android:textSize="15sp"
android:textColor="@color/white"
android:background="@drawable/shape_btn"/>
</LinearLayout>
这里布局有个问题,因为获取验证码这个TextView中的字会在倒计时期间有变化,而且每次字的变化长度不一样,如果把它的layout_width设为wrap_content,那么这个TextView的长度会变化,影响界面美观,所以可以把它的长度固定,然后把验证码输入框的layout_weight设为1,这样就可以了。
3、具体使用方法
// 发送成功进入倒计时
countDownTimer = new CountDownTimerUtils(tv_send_code, 60000, 1000);
countDownTimer.start();
其中60000代表要倒计时的时长,即60s;1000代表每次递减1s。


猜你喜欢
- 一、信号量(Semaphore)信号量(Semaphore)是由内核对象维护的int变量,当信号量为0时,在信号量上等待的线程会堵塞,信号量
- 1.创建字符串的方法1.1构造方式一、直接构造String str = "fly";方式二 、调用构造方法进行构造对象S
- Android MotionEvent中getX()和getRawX()的区别实例详解实例代码:public class Res exten
- 简介:在团队协作开发的过程中,好的代码管理能更加有效的使日常开发的过程中对各个开发人员提高开发速度。下面将详细介绍在IDEA中使用git提交
- 本文实例讲述了JAVA实现的简单万年历。分享给大家供大家参考,具体如下:import java.util.Scanner;public cl
- ***Source URL: http://i.yesky.com/bbs/jsp/view.jsp?articleID=889992&am
- 案例简述通过C#使用类似QQ窗体的功能,当窗体放置到屏幕的边缘,可以将窗体隐藏,当鼠标再次放置到屏幕边缘时,窗体可再次显示。预备知识导图功能
- Java注解介绍基于注解(Annotation-based)的Java开发无疑是最新的开发趋势.[译者注: 这是05年的文章,在2014年,
- SQLite 介绍SQLite,是一款轻型的数据库,用于本地的数据储存。先说说优点,它占用资源非常的低,在嵌入式设备中需要几百K的内存就够了
- Java中的set是无序的,但是是不可重复的HashSet底层是哈希表,通过调用hashcode和equals方法实现去重当我们HashSe
- 在上一篇文章中完成了 《Maven镜像地址大全 》,后来又花了时间又去收集并整理了关于 maven 远程仓库地址,并整理于此,关于 Mave
- springboot通过URL方式访问外部资源遇到这个问题时翻阅百度,无外乎就是两种方式第一种在springboot 2.1.8中该方法已过
- 有时候我们需要实现这样的场景,类似进入开发者模式,即多次点击后执行操作。首先我们先看一个方法:System提供的一个静态方法arraycop
- Java8Stream流操作List去重根据属性去重整体去重使用distinctArrayList<LabelInfoDTO>
- 关于DocumentCompleted事件,MSDN给出的解释是在文档加载完毕后执行,但是在我的程序中DocumentCompleted却被
- 本文实例讲述了Android编程简单实现雷达扫描效果。分享给大家供大家参考,具体如下:在eoe看到有一篇关于雷达扫描的文章,然后看了下,很简
- 今天提交代码的时候突然发现IDEA的SVN面板Version Control下面多出来这么一句话:顿时心里咯噔一下,这肯定不是好事啊。果然,
- 什么是RabbitMQ?RabbitMQ是由erlang语言开发的一个基于AMQP(Advanced Message Queuing Pro
- 我们在shader中对贴图处理时,有时候会有一些比较复杂的运算,比方说三角函数,开方等,一般情况下,如果可以在越上层做运算,性能会越高。C#
- 1、字符串数字之间的转换(1)string --> char * string str("OK");