Android封装实现短信验证码的获取倒计时
作者:萌动小彩笔 发布时间:2023-06-28 22:57:23
标签:Android,封装,倒计时,短信
如图所示的效果相信大家都不陌生,我们可以使用很多种方法去实现此效果,这里自己采用CountDownTimer
定时器简单封装下此效果,方便我们随时调用。
首页先在attrs.xml
中定义下所需的几个属性:
<resources>
<declare-styleable name="CountDownButton">
<attr name="millisinfuture" format="integer"/>
<attr name="countdowninterva" format="integer"/>
<attr name="normalColor" format="color"/>
<attr name="countDownColor" format="color"/>
</declare-styleable>
</resources>
下面是实现的具体代码,很简单的一种方式,通俗易懂:
/**
* Created by xiaolong on 2018/1/12.
*/
@SuppressLint("AppCompatCustomView")
public class CountDownButton extends Button{
//总时长
private long millisinfuture;
//间隔时长
private long countdowninterva;
//默认背景颜色
private int normalColor;
//倒计时 背景颜色
private int countDownColor;
//是否结束
private boolean isFinish;
//定时器
private CountDownTimer countDownTimer;
public CountDownButton(Context context) {
this(context,null);
}
public CountDownButton(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public CountDownButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CountDownButton,defStyleAttr,0);
//设置默认时长
millisinfuture = (long) typedArray.getInt(R.styleable.CountDownButton_millisinfuture,60000);
//设置默认间隔时长
countdowninterva = (long)typedArray.getInt(R.styleable.CountDownButton_countdowninterva,1000);
//设置默认背景色
normalColor = typedArray.getColor(R.styleable.CountDownButton_normalColor,android.R.color.holo_blue_light);
//设置默认倒计时 背景色
countDownColor = typedArray.getColor(R.styleable.CountDownButton_countDownColor,android.R.color.darker_gray);
typedArray.recycle();
//默认为已结束状态
isFinish = true;
//字体居中
setGravity(Gravity.CENTER);
//默认文字和背景色
normalBackground();
//设置定时器
countDownTimer = new CountDownTimer(millisinfuture, countdowninterva) {
@Override
public void onTick(long millisUntilFinished) {
//未结束
isFinish = false;
setText((Math.round((double) millisUntilFinished / 1000) - 1) + "秒");
setBackgroundResource(countDownColor);
}
@Override
public void onFinish() {
//结束
isFinish = true;
normalBackground();
}
};
}
private void normalBackground(){
setText("获取验证码");
setBackgroundResource(normalColor);
}
public boolean isFinish() {
return isFinish;
}
public void cancel(){
countDownTimer.cancel();
}
public void start(){
countDownTimer.start();
}
}
一个简单的调用方式:
public class MainActivity extends AppCompatActivity {
private CountDownButton countDownButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countDownButton = ((CountDownButton) findViewById(R.id.countDownButton));
countDownButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//这里判断是否倒计时结束,避免在倒计时时多次点击导致重复请求接口
if (countDownButton.isFinish()) {
//发送验证码请求成功后调用
countDownButton.start();
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (!countDownButton.isFinish()) {
countDownButton.cancel();
}
}
}
这样一个简单的封装就结束了,过程很简单。这里主要是对CountDownTimer
的使用练习,之前工作中一直没有接触过这个类。
来源:https://blog.csdn.net/zl_china/article/details/79085093
0
投稿
猜你喜欢
- PS:本文包含了大部分strings函数的说明,并附带举例说明。本来想自己整理一下的,发现已经有前辈整理过了,就转了过来。修改了原文一些源码
- 关于UIToolbarToolBar工具栏是视图View的属性,可以在工具栏上添加工具栏按钮Bar Button Item(可以是自定义的C
- 接触过Android开发的同学们都知道在Android中访问程序资源基本都是通过资源ID来访问。这样开发起来很简单,并且可以不去考虑各种分辨
- 已知字符串“aabbbcddddeeffffghijklmnopqrst”编程找出出现最多的字符和次数,要求时间复杂度小于O(n^2)/**
- 方式一:if语句控制// 例如:Column( mainAxisAlig
- 使用AS创建ADIL文件时AS会在main文件夹下给我们生成一个aidl文件夹和一个相同包名的包,通常我们会把所有和ADIL相关的类或文件放
- 1. strlen —— 求字符串长度1.1 strlen 的声明与用处strlen ,我们有一些英
- 相比于直线检测,直线拟合的最大特点是将所有数据只拟合出一条直线void fitLine( InputArray points, Output
- BitArray的基础可以看菜鸟编程BitArray 类管理一个紧凑型的位值数组,它使用布尔值来表示,其中 true 表示位是开启的(1),
- 合成聚合复用原则合成复用原则又称为组合/聚合复用原则(Composition/Aggregate Reuse Principle, CARP
- 目录连接远程主机Remote Development插件安装远程主机命令行arm 远程调试VSCode 配置arm 上运行 gdb serv
- [LeetCode] 5. Longest Palindromic Substring 最长回文子串Given a string
- 前言我们在学习Windows应用程序开发中,经常会用到消息对话框给用户或者管理员一些的消息提示,它们都是基于对MessageBox类的消息对
- 最近没事写了一个简易浏览器,在刚开始写的时候遇到一些问题,主要的问题就是如何在自己的webview中显示所有的网页数据,不过不指
- Console.WriteLine("This is a Client, host name is {0}", Dns.
- 废话开篇:iOS与android在实现列表界面的时候是有重用机制的,目的就是减少内存开销,用时间换空间。个人感觉flutter并没有特别强调
- 前言通过此篇文章,你将了解到:Flutter windows和Android桌面应用屏幕适配的解决方案;屏幕适配的相关知识和原理;flutt
- 一、项目要求实现一个通讯录通讯录可以用来存储100个人的信息,每个人的信息包括:姓名、性别、年龄、电话、住址提供方法:添加联系人信息删除指定
- ImageCacheconst int _kDefaultSize = 1000;const int _kDefaultSizeBytes
- 在我们实现某些功能时,可能会有倒计时的需求。比如发送短信验证码,发送成功后可能要求用户一段时间内不能再次发送,这时候我们就需要进行倒计时,时