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。
0
投稿
猜你喜欢
- C#实现的获取路由器MAC地址,路由器外网地址。对于要获取路由器MAC地址,一定需要知道路由器web管理系统的用户名和密码。至于获取路由器的
- WebService是一种跨编程语言和跨操作系统平台的远程调用技术所谓远程调用,就是一台计算机a上的一个程序可以调用到另外一台计算机b上的一
- 本文实例为大家分享了Unity实现俄罗斯方块第3部分,供大家参考,具体内容如下解决穿透问题逻辑部分1、在物体进行移动的过程中更新格子的信息,
- 文件下载这种事情是很耗时的。之前使用AsyncTask这样的异步类来做下载,然后切到后台就 * 掉。所以打算试试Service。(不过按目前那
- SpringCloud Zuul 是SpringCloud系列的网关实现,具有均衡负载,将非业务性校验剥离出来,使微服务专注于业务的一个组件
- 前言本文主要给大家介绍了关于Spring中@Scheduled和HttpClient的坑,分享出来供大家参考学习,下面话不多说了,来一起看看
- 本文实例讲述了基于JavaMail API收发邮件的方法。分享给大家供大家参考。具体如下:1.JavaMail API按其功能划分通常可分为
- 里氏替换原则(LSP)定义:在任何父类出现的地方都可以用它的子类类替换,且不影响功能。解释说明:其实LSP是对开闭原则的一个扩展,在OO思想
- JAVA中的类只能是public 或者package的。这是符合逻辑的:人们定义类的初衷就是为了让别人用的。倘若是private,别人怎么调
- 读取xml配置bean(@ImportResource)1、应用场景旧框架SSM项目移行到SpringBoot中,xml配置文件很齐全,就可
- 话说,最近一次系统维护 用JS读取导入Excel中的实验数据,出现被自动四舍五入。后来到客户现场听客户反馈 Excel实验数据要
- 归纳一些网上取JAVA路径的方法: 注明:如果从ANT启动程序,this.getClass().getResource("&quo
- 简单工厂模式介绍:概要:简单工厂模式,又称为静态工厂方法(Static Factory Method)模式,它属于类创建型模式。在简单工厂模
- 并发与并行:并行: 指两个或多个事件在同一时刻发生 ( 同时发生 ) 。并发: 指两个或多个事件在同一个时间段内发生。多线程使用场景:1.
- 简介MapStruct 是一个代码生成器(可以生成对象映射转换的代码),它基于约定优于配置的方法,极大地简化了 Java bean 类型之间
- 在微信公众号支付的API中没有这个接口,如果企业需要给用户转账,或者让用户提现或者给用户发红包等需要再商户平台中的产品中心分别开通。一、开通
- 废话不多说了,关键代码如下所示:import java.util.*;public class Demo04 {public static
- 汉诺塔游戏一旦掌握了规律,其实是有点单调和无聊的,不过却是学习递归的一个绝佳例子,想当初学习老谭C的时候,就卡在这儿好长时间。对初学编程的人
- 一.什么是maven?Maven是一个项目管理工具,它包含了一个项目对象模型 (Project Object Model),一组标准集合,一
- EasyCode 插件EasyCode 插件 是一款根据表结构生成代码的很方便的Idea插件, 强烈推荐. 并且可以自定义模板来控制生成的类