Android获取验证码倒计时功能(CountDownTimer实现)
来源:互联网 发布:三国杀关索淘宝 编辑:程序博客网 时间:2024/05/04 16:10
activity_main.xml文件:
<Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="获取验证码" android:background="@drawable/btn_drawable"/>
res-drawable-btn_drawable.xml:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="8px"></corners> <stroke android:width="1px" android:color="#e4e4e4"></stroke></shape>
MainActivity:
private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { CountDownTimerUtil countDownTimerUtil = new CountDownTimerUtil(button,60000,1000); countDownTimerUtil.start(); } }); }
写一个CountDownTimerUtil类继承CountDownTimer类实现倒计时功能:
public class CountDownTimerUtil extends CountDownTimer { private Button button; public CountDownTimerUtil(Button button, long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); this.button = button; } //点击时触发这个操作 @Override public void onTick(long l) { button.setText(l / 1000 + "秒倒计时"); button.setClickable(false); button.setBackgroundColor(Color.parseColor("#e88d94")); } //倒计时完成以后 @Override public void onFinish() { button.setClickable(true); button.setText("重发验证码"); button.setBackgroundResource(R.drawable.btn_drawable); }}
CountDownTimerUtil的两个参数分别是总时间和倒计时间隔时间:倒计时60秒 每秒执行一次
好了,效果实现了点击按钮开始倒计时功能。
0 0
- Android获取验证码倒计时功能(CountDownTimer实现)
- android登陆获取验证码倒计时功能的实现(CountDownTimer)
- Android获取验证码倒计时CountDownTimer
- 注册时手机获取验证码倒计时,使用CountDownTimer类轻松实现倒计时功能
- Android CountDownTimer 实现倒计时功能
- Android获取验证码时的倒计时 CountDownTimer使用
- Android使用CountDownTimer实现短信验证码倒计时
- 获取短信验证码 倒计时 CountDownTimer
- android倒计时功能的实现(CountDownTimer)
- Android利用CountDownTimer类实现倒计时功能
- android倒计时功能的实现(CountDownTimer)
- android倒计时功能的实现(CountDownTimer)
- android倒计时功能的实现(CountDownTimer)
- android倒计时功能的实现(CountDownTimer)
- android倒计时功能的实现(CountDownTimer)
- Android使用CountDownTimer实现倒计时功能
- Android倒计时功能的实现(CountDownTimer)
- Android 倒计时功能的实现(CountDownTimer)
- POJ 3388 Japanese Puzzle(二分法)
- Springboot Mybatis 打包jar扫描bean与mapper问题研究与解决
- 字节对齐详解
- java-csp-股票波动问题
- Chrome macOS 快捷键 from Google Support
- Android获取验证码倒计时功能(CountDownTimer实现)
- 在Linux 之redhat/debian两类系统上源码成功安装tensorflow填坑记录
- USACO 2.1 顺序的分数 (枚举)
- android 中ContentObserver和DataSetObserver的使用
- 字符串
- java当中的定时器的4种使用方式
- 20.大型网站典型故障分析
- 协同过滤中item-based与user-based选择依据
- USACO 3.2 Sweet Butter 香甜的黄油 (Bellman DIJ SPFA)