Android开发之获取短信验证码后按钮背景变化并且出现倒计时
目前越来越多的app在注册或是进行对应操作时,要求获取短信验证码,在点击了获取短信验证码的按钮后,就是出现倒计时,比如倒计时120S,在倒计时期间内,按钮背景变化并且出现倒计时,当倒计时结束后,如果你没有获取到验证码,可以再次点击。
代码如下所示:
VerCodeTimer mVerCodeTimer=(Button) findViewById(R.id.login_get_ver_code);
private class VerCodeTimer extends CountDownTimer {
private int seconds;
private int interval;
//millisInFuture为你设置的此次倒计时的总时长,比如60秒就设置为60000
//countDownInterval为你设置的时间间隔,比如一般为1秒,根据需要自定义。
public VerCodeTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
seconds = (int) (millisInFuture / 1000);
interval= (int) (countDownInterval/1000);
}
//每过你规定的时间间隔做的操作
@Override
public void onTick(long millisUntilFinished) {
getVerCodeButton.setText((seconds-interval) + "秒后重新获取");
}
//倒计时结束时做的操作↓↓
@Override
public void onFinish() {
getVerCodeButton.setTextSize(10);
getVerCodeButton.setText("重新获取验证码");
getVerCodeButton.setClickable(true);
getVerCodeButton.setBackgroundResource(R.drawable.login_get_ver_code_before_bg);
}
}
@Override
public void onBackPressed() {
if (mVerCodeTimer != null)
mVerCodeTimer.cancel();
super.onBackPressed();
}
使用的时候:
getVerCodeButton.setTextSize(11); getVerCodeButton.setClickable(false); getVerCodeButton.setBackgroundResource(R.drawable.login_get_ver_code_ago_bg); mVerCodeTimer = new VerCodeTimer(60000, 1000); mVerCodeTimer.start();
login_edit_normal_bg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:useLevel="false">
<!-- 背景填充颜色值 -->
<solid android:color="#6c948b" />
<!-- radius值越大,越趋于圆形 -->
<corners android:radius="10dip" />
<!-- 圆角图像内部填充四周的大小 ,将会以此挤压内部布置的view -->
<padding
android:bottom="10dip"
android:left="10dip"
android:right="10dip"
android:top="10dip" />
</shape>
login_edit_passed_bg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:useLevel="false">
<!-- 背景填充颜色值 -->
<solid android:color="#509989" />
<!-- radius值越大,越趋于圆形 -->
<corners android:radius="10dip" />
<!-- 圆角图像内部填充四周的大小 ,将会以此挤压内部布置的view -->
<padding
android:bottom="10dip"
android:left="10dip"
android:right="10dip"
android:top="10dip" />
</shape>
以上所述是小编给大家介绍了Android开发之获取短信验证码后按钮背景变化并且出现倒计时 的全部代码,希望本段代码能够帮助大家。同时感谢大家一直以来对脚本之家网站的支持。
相关文章
Android 中 requestWindowFeature()的应用
本文主要介绍 Android requestWindowFeature()方法,这里对 requestWindowFeature()方法进行详解,对应用程序窗体显示状态的操作有进一步了解,希望能帮助有需要的小伙伴2016-07-07
Android TextView实现点击显示全文与隐藏功能(附源码)
TextView用法很多,用到的地方更是普遍,所以学好TextView的使用很重要很重要很重要。下面这篇文章主要介绍了Android中TextView实现显示全文与隐藏功能的相关资料,文中给出了详细的示例代码和源码下载,需要的朋友可以参考下。2017-03-03
android与asp.net服务端共享session的方法详解
这篇文章主要给大家介绍了关于android与asp.net服务端如何共享session的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋们下面随着小编来一起学习学习下吧。2017-09-09
Android基础之startActivityForResult()的用法详解
这篇文章主要给大家介绍了Android中startActivityForResult()的用法,文中给出了详细的介绍与示例代码,相信对大家的理解和学习具有一定参考借鉴价值,有需要的朋友们下面来一起看看吧。2017-01-01
Android Activity启动模式之standard实例详解
这篇文章主要介绍了Android Activity启动模式之standard,结合实例形式较为详细的分析了Android中活动(Activity)四种启动模式中的standard相关注意事项与实现技巧,需要的朋友可以参考下2016-01-01


最新评论