Android实现获取验证码的倒计时功能

本文介绍了一种在Android应用中实现短信验证码倒计时功能的方法。通过自定义CountDownTimer类,可以轻松地在不同场景下复用,并且支持自定义按钮文本颜色和倒计时结束后的显示文本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android 获取短信验证码倒计时

\

首先看下我们封装的倒计时工具类,主要为了在多个地方用到的话,用了多个构造方法,就是为了使用更灵活,只要传入对数就可以调用了:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
public class MyCountTimer extends CountDownTimer {
public static final int TIME_COUNT = 121000 ; //时间防止从119s开始显示(以倒计时120s为例子)
private TextView btn;
private int endStrRid;
private int normalColor, timingColor; //未计时的文字颜色,计时期间的文字颜色
 
/**
* 参数 millisInFuture         倒计时总时间(如60S,120s等)
* 参数 countDownInterval    渐变时间(每次倒计1s)
 
         * 参数 btn               点击的按钮(因为Button是TextView子类,为了通用我的参数设置为TextView)
 
         * 参数 endStrRid   倒计时结束后,按钮对应显示的文字
*/
public MyCountTimer ( long millisInFuture, long countDownInterval, TextView btn, int endStrRid) {
super (millisInFuture, countDownInterval);
this .btn = btn;
this .endStrRid = endStrRid;
}
 
 
/**
 
           *参数上面有注释
*/
public  MyCountTimer (TextView btn, int endStrRid) {
super (TIME_COUNT, 1000 );
this .btn = btn;
this .endStrRid = endStrRid;
}
 
public MyCountTimer (TextView btn) {
super (TIME_COUNT, 1000 );
this .btn = btn;
this .endStrRid = R.string.txt_getMsgCode_validate;
}
 
 
public MyCountTimer (TextView tv_varify, int normalColor, int timingColor) {
this (tv_varify);
this .normalColor = normalColor;
this .timingColor = timingColor;
}
 
// 计时完毕时触发
@Override
public void onFinish() {
if (normalColor > 0 ){
btn.setTextColor(normalColor);
}
btn.setText(endStrRid);
btn.setEnabled( true );
}
 
// 计时过程显示
@Override
public void onTick( long millisUntilFinished) {
if (timingColor > 0 ){
btn.setTextColor(timingColor);
}
btn.setEnabled( false );
btn.setText(millisUntilFinished / 1000 + "s" );
}
}


然后在你要实现倒计时的页面用就可以了:

比如在AcvitityA中点击倒时间的按钮

Button smsBtn=findViewById(R.id.....);

 MyCountTimertimeCount = new MyCountTimer(smsBtn, 0xfff30008, 0xff969696);//传入了文字颜色值
timeCount.start();

如时你不传入颜色值的话,也可以在点击按钮smsBtn的布局文件中根据按钮状态来设置颜色。

 

?
1
2
3
4
5
6
7
8
9
10
11
< Button
             android:id = "@+id/rebind_sms_btn"
             android:layout_width = "120dp"
             android:layout_height = "45dp"
             android:layout_marginLeft = "5dp"
             android:layout_marginRight = "5dp"
             android:background = "@null"
             android:gravity = "center"
             android:text = "获取短信验证码"
             android:textColor = "@color/hkb_binder_phone_text_color"
             android:textSize = "16sp" />



文字颜色对应的xml文件:

?
1
2
3
4
5
<? xml version = "1.0" encoding = "utf-8" ?>
< selector xmlns:android = "http://schemas.android.com/apk/res/android" >
     < item android:state_enabled = "false" android:color = "#969696" />
     < item android:color = "#f30008" />
</ selector >


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值