Android利用CountDownTimer实现点击获取验证码倒计时效果
作者:LoaderMan 时间:2023-09-09 21:09:54
本文实例为大家分享了Android点击获取验证码倒计时的具体代码,供大家参考,具体内容如下
package com.loaderman.countdowntimerdemo;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tvTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvTime = (TextView) findViewById(R.id.tv_time);
tvTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new countDownTimer<TextView>(tvTime);
}
});
}
public class countDownTimer<T> extends CountDownTimer {
private TextView mTextView;
public countDownTimer(T text) {
super(60000,1000);
this.mTextView = ((TextView)text);
mTextView.setClickable(false);
start();
}
/**
* start后开始执行的方法,按照间隔时间执行一次
* @param millisUntilFinished
*/
@Override
public void onTick(long millisUntilFinished) {
mTextView.setText(String.format("%s秒",millisUntilFinished/1000));
}
/**
* 时间结束时调用的方法
*/
@Override
public void onFinish() {
mTextView.setText("获取验证码");
mTextView.setClickable(true);
}
}
}
xml代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context="com.loaderman.countdowntimerdemo.MainActivity">
<TextView
android:textColor="#F00"
android:textSize="18sp"
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始倒计时"/>
</RelativeLayout>
效果图:
标签:Android,CountDownTimer,倒计时
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java关键字instanceof用法及实现策略
2023-09-23 06:05:38
![](https://img.aspxhome.com/file/2023/0/58560_0s.png)
理解maven命令package、install、deploy的联系与区别
2022-08-09 05:11:39
![](https://img.aspxhome.com/file/2023/4/116134_0s.png)
jar包运行时提示jar中没有主清单属性的解决
2023-11-23 19:04:10
![](https://img.aspxhome.com/file/2023/2/59532_0s.png)
springboot中自定义异常以及定制异常界面实现过程解析
2023-05-13 09:14:00
![](https://img.aspxhome.com/file/2023/3/105913_0s.png)
Java中的这些骚操作你不能不知道!!!
2022-07-08 12:28:14
![](https://img.aspxhome.com/file/2023/4/60574_0s.png)
Android仿360桌面手机卫士悬浮窗效果
2021-06-16 10:06:37
![](https://img.aspxhome.com/file/2023/9/88719_0s.png)
Android提高之使用NDK把彩图转换灰度图的方法
2023-07-22 02:00:07
![](https://img.aspxhome.com/file/2023/6/88686_0s.jpg)
java模拟斗地主发牌功能
2023-06-26 08:01:34
mybatis如何使用Criteria的and和or进行联合查询
2023-02-23 00:44:13
SpringBoot之webflux全面解析
2022-09-27 12:07:20
![](https://img.aspxhome.com/file/2023/3/115903_0s.jpg)
C语言手把手带你掌握带头双向循环链表
2023-01-14 16:52:59
![](https://img.aspxhome.com/file/2023/5/127495_0s.jpg)
Android PowerManagerService省电模式策略控制
2023-11-25 02:46:53
C#窗体编程不显示最小化、最大化、关闭按钮的方法
2023-03-03 00:04:29
c#批量抓取免费代理并且验证有效性的实战教程
2023-12-19 23:33:30
![](https://img.aspxhome.com/file/2023/7/129657_0s.gif)
C#中的枚举类型(Enum)介绍
2023-06-23 18:20:06
详解基于Android App 安全登录认证解决方案
2022-12-28 04:45:18
![](https://img.aspxhome.com/file/2023/5/125445_0s.png)
Android编程之高效开发App的10个建议
2021-08-28 15:55:46
JAVA实现扫描线算法(超详细)
2023-06-30 13:33:20
![](https://img.aspxhome.com/file/2023/3/78333_0s.jpg)
Android中Listview点击item不变颜色及设置listselector 无效的解决方案
2022-07-04 23:49:10
Java中ArrayList和LinkedList之间的区别_动力节点Java学院整理
2023-03-30 18:16:29