Android快速自定义倒计时按钮
来源:互联网 发布:网络划分的策略 编辑:程序博客网 时间:2024/06/02 05:31
现在几乎所有的app都有个倒计时按钮,注册,和一些比较需要识别身份的验证都需要这个按钮。so现在我们简单的打造一个速成倒计时按钮先看下效果
定时器
import java.util.Timer;import java.util.TimerTask;
代码
package com.kekegdsz.countdownbutton;import android.annotation.TargetApi;import android.content.Context;import android.os.Build;import android.os.Handler;import android.os.Message;import android.text.TextUtils;import android.util.AttributeSet;import android.view.View;import android.widget.Button;import java.util.Timer;import java.util.TimerTask;/** * Created by Administrator on 2016/10/26. */public class CountDownButton extends Button implements View.OnClickListener { /*默认倒计时长*/ private long countDownLength = 60 * 1000; /*未点击之前的显示的文字*/ private String beforeText = "获取验证码"; /*倒计时结束后获取*/ private String refreshText = "重新获取"; /** * 开始执行计时的类,可以在每秒实行间隔任务 */ private Timer timer; /** * 在开始倒计时之后那个秒数数字之后所要显示的字,默认是秒 */ private String afterText = "秒"; /** * 按钮点击事件 */ private OnClickListener onClickListener; /** * 每秒时间到了之后所执行的任务 */ private TimerTask timerTask; public CountDownButton(Context context) { super(context); init(); } public CountDownButton(Context context, AttributeSet attrs) { super(context, attrs); init(); } public CountDownButton(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public CountDownButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); init(); } private void init() { if (!TextUtils.isEmpty(getText())) { beforeText = getText().toString().trim(); } this.setText(beforeText); this.setOnClickListener(this); } @Override public void onClick(View view) { start(); if (onClickListener != null) { onClickListener.onClick(view); } } /** * 设置监听按钮点击事件 * * @param onclickListener */ @Override public void setOnClickListener(OnClickListener onclickListener) { if (onclickListener instanceof CountDownButton) { super.setOnClickListener(onclickListener); } else { this.onClickListener = onclickListener; } } /** * 开始倒计时 */ public void start() { initTimer(); this.setText(countDownLength / 1000 + afterText); this.setEnabled(false); timer.schedule(timerTask, 0, 1000); } /** * 初始化时间 */ private void initTimer() { timer = new Timer(); timerTask = new TimerTask() { @Override public void run() { handler.sendEmptyMessage(1); } }; } /** * 设置倒计时时长 * * @param length 默认毫秒 */ public void setLength(long length) { this.countDownLength = length; } /** * 设置未点击时显示的文字 * * @param beforeText */ public void setBeforeText(String beforeText) { this.beforeText = beforeText; } /** * 设置未点击后显示的文字 * * @param beforeText */ public void setAfterText(String beforeText) { this.afterText = afterText; } /** * 更新显示的文本 */ private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); CountDownButton.this.setText(countDownLength / 1000 + afterText); countDownLength -= 1000; if (countDownLength < 0) { CountDownButton.this.setEnabled(true); CountDownButton.this.setText(refreshText); clearTimer(); countDownLength = 60 * 1000; } } }; /** * 清除倒计时 */ private void clearTimer() { if (timerTask != null) { timerTask.cancel(); timerTask = null; } if (timer != null) { timer.cancel(); timer = null; } } /** *在界面销毁后调用,否则有内存泄漏风险 */ @Override protected void onDetachedFromWindow() { clearTimer(); super.onDetachedFromWindow(); }}
使用
<com.kekegdsz.countdownbutton.CountDownButton android:layout_width="wrap_content" android:textColor="#232323" android:layout_height="wrap_content" />
/** *在界面销毁后调用,否则有内存泄漏风险 */ @Override protected void onDetachedFromWindow() { clearTimer(); super.onDetachedFromWindow(); }
1 0
- Android快速自定义倒计时按钮
- Android自定义一个倒计时按钮
- Android自定义控件之自定义倒计时按钮
- Android自定义TimeButton实现倒计时按钮
- 自定义倒计时按钮
- 短信倒计时自定义按钮
- 自定义倒计时跳过按钮
- 【Android自定义View实战】之获取验证码倒计时按钮
- android 按钮倒计时读秒
- 简单的自定义倒计时按钮
- 自定义倒计时按钮swift版
- react-native 自定义倒计时按钮
- Android快速实现倒计时
- Android自定义倒计时控件
- Android自定义倒计时控件
- Android自定义倒计时控件
- Android自定义控件 倒计时
- Android实现倒计时的按钮
- 判断闰年
- 一次完整的HTTP请求所经历的7个步骤
- 马云---下一个三十年企业家最重要的素质是什么?
- 【PAT甲级】1049. Counting Ones (30)
- django 1.8 官方文档翻译:logging
- Android快速自定义倒计时按钮
- EasyDarwin+ffmpeg进行PC(摄像头+麦克风)流媒体直播服务
- Volley尝试源码解析1
- 关于adb端口被占用
- (2016弱小联盟十一专场10.3) Help the Princess! BFS
- 学习:实现任意角度的sin、cos求值-No.1
- 正则表达式判断手机号
- 如何控制秒杀商品页面购买按钮的定时点亮
- Go语言编程入门