Android实现点击获取验证码60秒后重新获取功能
作者:Xia_焱 时间:2021-09-21 22:42:01
本文实例为大家分享了Android实现点击获取验证码60秒后重新获取的具体代码,供大家参考,具体内容如下
上代码
/**
* Created by Xia_焱 on 2017/5/7.
*/
public class CountDownTimerUtils extends CountDownTimer {
private TextView mTextView;
/**
* @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 receive
* {@link #onTick(long)} callbacks.
*/
public CountDownTimerUtils(TextView textView, long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
this.mTextView = textView;
}
@Override
public void onTick(long millisUntilFinished) {
mTextView.setClickable(false); //设置不可点击
mTextView.setText(millisUntilFinished / 1000 + "秒后可重新发送"); //设置倒计时时间
mTextView.setBackgroundResource(R.drawable.bg_identify_code_press); //设置按钮为灰
SpannableString spannableString = new SpannableString(mTextView.getText().toString());
ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
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.bg_identify_code_normal);
}
}
TextView背景色
bg_identify_code_press
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#C0C0C0" /> <!--填充色 透明-->
<corners android:radius="7dp" /> <!-- 圆角 -->
</shape>
bg_identify_code_normal
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#2BAF2B" /> <!--填充色 透明-->
<corners android:radius="7dp" /> <!-- 圆角 -->
</shape>
布局代码
<TextView
android:id="@+id/tv_yzm"
android:layout_width="match_parent"
android:layout_marginTop="50dp"
android:layout_height="45dp"
android:background="@drawable/bg_identify_code_normal"
android:gravity="center"
android:text="点击获取验证码"
android:textColor="#FFF"
android:textSize="15dp" />
项目代码
private void initView() {
tv_yzm = (TextView) findViewById(R.id.tv_yzm);
tv_yzm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(tv_yzm, 60000, 1000);
mCountDownTimerUtils.start();
}
});
}
效果图
来源:https://blog.csdn.net/MacaoPark/article/details/71379520
标签:Android,验证码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
maven实现jar包导入+导出方式
2023-12-13 03:32:22
![](https://img.aspxhome.com/file/2023/2/67142_0s.png)
Java集合Map的clear与new Map区别详解
2022-03-21 15:41:39
![](https://img.aspxhome.com/file/2023/1/63321_0s.png)
Java面向对象选择题总结归纳
2023-08-05 23:30:21
Java如何提供给第三方使用接口方法详解
2022-07-22 19:59:19
Android TabLayout设置指示器宽度的方法
2023-03-27 02:53:05
![](https://img.aspxhome.com/file/2023/0/109180_0s.png)
C# 最基础知识介绍--多态
2022-07-10 14:34:36
![](https://img.aspxhome.com/file/2023/1/79111_0s.jpg)
Java线程池的几种实现方法和区别介绍
2023-12-12 12:46:04
浅析JDK和Tomcat的安装与配置方法
2022-02-02 07:49:43
利用java开发简易版扫雷游戏
2023-11-07 14:35:15
![](https://img.aspxhome.com/file/2023/9/63489_0s.png)
java中断线程的正确姿势完整示例
2023-01-27 06:33:58
![](https://img.aspxhome.com/file/2023/8/67648_0s.jpg)
Java的类型擦除式泛型详解
2022-01-02 11:59:00
![](https://img.aspxhome.com/file/2023/8/61608_0s.jpg)
Android实现登录界面的注册功能
2021-10-22 01:17:55
![](https://img.aspxhome.com/file/2023/3/106873_0s.jpg)
C语言中输入输出流与缓冲区的深入讲解
2021-06-12 07:44:29
![](https://img.aspxhome.com/file/2023/5/107405_0s.jpg)
Spring 应用中集成 Apache Shiro的方法
2023-02-18 05:44:55
解决IDEA和CMD中java命令提示错误: 找不到或无法加载主类的问题
2023-09-19 02:31:16
![](https://img.aspxhome.com/file/2023/2/101922_0s.png)
Java后端Tomcat实现WebSocket实例教程
2023-08-22 17:31:09
![](https://img.aspxhome.com/file/2023/0/58340_0s.png)
为IObservable实现自己的运算符(详解)
2022-04-02 08:58:15
Java时间工具类Date的常用处理方法
2022-11-08 02:37:49
Qt之调用C#的动态库的解决方法
2023-07-22 03:42:45
![](https://img.aspxhome.com/file/2023/1/67061_0s.png)
Java基于Tcp/ip连接的多人交互聊天室
2022-03-31 17:47:35
![](https://img.aspxhome.com/file/2023/6/103846_0s.png)