安卓开发之显示倒计时按钮控件
2016-08-05 22:49
288 查看
实现倒计时逻辑比较简单,使用了CountDownTimer来计时(源码考虑了线程安全问题)。
对使用逻辑封装成了一个自定义控件TimerButton。
Github:github地址
对使用逻辑封装成了一个自定义控件TimerButton。
源代码:
import android.content.Context; import android.os.CountDownTimer; import android.util.AttributeSet; import android.widget.Button; /** * Created by cxm on 2016/8/5. */ public class TimerButton extends Button { private String beforeText ; private String afterText = "重发"; private int ms = 10000; public TimerButton(Context context) { super(context); } public TimerButton(Context context, AttributeSet attrs) { super(context, attrs); } public TimerButton(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public void init(String beforeText,String afterText,int ms){ this.beforeText = beforeText; this.afterText = afterText; this.ms = ms; this.setText(beforeText); } public void startTimer(){ new CountDownTimer(ms,1000){ @Override public void onTick(long finish) { TimerButton.this.setEnabled(false); TimerButton.this.setText(finish/1000+" s"); } @Override public void onFinish() { TimerButton.this.setEnabled(true); TimerButton.this.setText(afterText); } }.start(); } }
使用:
private TimerButton timerButton; timerButton = (TimerButton) findViewById(R.id.timer_Button); timerButton.init("获取验证码","重发",10000); timerButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { timerButton.startTimer(); //TODO YOURS //Toast.makeText(MainActivity.this,"TODO",Toast.LENGTH_SHORT).show(); } });
Github:github地址
相关文章推荐
- 安卓开发中的双日期选择控件(可隐藏日,只显示年月)
- [Phonegap+Sencha Touch] 移动开发48 扩展的NavigationView控件,支持根据不同的View显示不同的顶栏按钮、不同的屏幕方向,增加视图缓存机制
- 两个并排的按钮只显示了一个【安卓开发布局问题 wrap_content】
- XE8开发安卓之二:按钮和 TEdit 控件的几个常用的属性
- 安卓开发启用线程动态显示和隐藏悬浮控件(ImageButton),点击回到页面顶部
- 安卓开发中的双日期选择控件(可隐藏日,只显示年月)
- 安卓开发添加依赖后,提示类找不到异常,控件不显示
- 安卓开发_单选按钮控件(RadioButton)的简单使用
- 开发第一个安卓程序(按钮显示一个当前的系统时间)
- 安卓开发中的双日期选择控件(可隐藏日,只显示年月)
- 安卓开发_复选按钮控件(CheckBox)的简单使用
- 安卓开发中的双日期选择控件(可隐藏日,只显示年月)
- 安卓开发中的双日期选择控件(可隐藏日,只显示年月)
- 安卓开发中的双日期选择控件(可隐藏日,只显示年月)
- ARCGIS Extending Objects开发小控件限制条件---如果没有图层按钮显示灰色
- 安卓开发中,listview数量过多,加更多按钮显示
- asp.net控件开发(一)-显示控件内容
- 隐藏和显示按钮控件。
- 单选按钮显示隐藏控件
- Delphi 组件开发教程指南(7)继续模拟动画显示控件