Android获取短信验证码倒计时
发布时间:2020-05-24 18:39:15 所属栏目:Java 来源:互联网
导读:Android获取短信验证码倒计时
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 目前越来越多的app在注册或是进行对应操作时,要求获取短信验证码,在点击了获取短信验证码的按钮后,就是出现倒计时,比如倒计时120S,在倒计时 期间内,按钮点击是无效的,当倒计时结束后,如果你没有获取到验证码,可以再次点击。实现倒计时的方法很多,我们今天就通过继承 android. os.CountDownTimer类来实现!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");
}
}
<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 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>
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
