admin 管理员组文章数量: 1086019
2024年3月14日发(作者:莱切斯特对利物浦)
Android获取短信验证码倒计时
目前越来越多的app在注册或是进行对应操作时,要求获取短信验证码,在点击了获取短
信验证码的按钮后,就是出现倒计时,比如倒计时120S,在倒计时 期间内,按钮点击是无
效的,当倒计时结束后,如果你没有获取到验证码,可以再次点击。实现倒计时的方法很多,
我们今天就通过继承
android.
ownTimer类来实现!
首先看下我们封装的倒计时工具类,主要为了在多个地方用到的话,用了多个构造方
法,就是为了使用更灵活,只要传入对数就可以调用了:
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);
= btn;
Rid = endStrRid;
public MyCountTimer (TextView tv_varify, int normalColor, int timingColor) {
this(tv_varify);
Color = normalColor;
Color = timingColor;
// 计时完毕时触发
@Override
public void onFinish() {
if(normalColor 0){
tColor(normalColor);
t(endStrRid);
bled(true);
// 计时过程显示
@Override
public void onTick(long millisUntilFinished) {
if(timingColor 0){
tColor(timingColor);
bled(false);
t(millisUntilFinished / 1000 + s
}
然后在你要实现倒计时的页面用就可以了:
比如在AcvitityA中点击倒时间的按钮
Button smsBtn=findViewById(.....);
MyCountTimertimeCount = new MyCountTimer(smsBtn, 0xfff30008, 0xff969696);//传入了文
字颜色值
();
如时你不传入颜色值的话,也可以在点击按钮smsBtn的布局文件中根据按钮状态来设置
颜色。
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文件:
xml version= 1.0 encoding= utf-8
selector xmlns:android= /apk/res/android
item android:state_enabled= false android:color= #969696 /
item android:color= #f30008 /
/selector
版权声明:本文标题:android获取短信验证码倒计时 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1710390057a570924.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论