首页 > 代码库 > Android 短信验证码控件
Android 短信验证码控件
Android 短信验证码控件,便于项目中使用统一样式,统一提示修改,个人认为挺好用的
<span style="font-size:18px;">public class SMSButton extends Button { /** 是否展示 */ private boolean mAttached; /** 发送验证码后等待的时间 */ private int delayTime = 60; /** Button 上面的文本 */ private String showText = "获取"; /** Button 等待的时候显示的文本 */ private String delayText = FORMATETAG + "秒"; /** 现在是否在倒计时 */ private boolean isDelayed = false; private SMSOnClickListener clickListener; private int currentDelayTime = 0; private static final String FORMATETAG = "%d"; private Runnable mTimer = new Runnable() { @Override public void run() { currentDelayTime -= 1; if (currentDelayTime <= 0) { isDelayed = false; setClickable(true); setText(showText); } else { getHandler().postDelayed(mTimer, 1000); setDelayTimeText(currentDelayTime); } } }; private void setDelayTimeText(int time) { setText(String.format(delayText, time)); } public SMSButton(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(); } public SMSButton(Context context, AttributeSet attrs) { super(context, attrs); initView(); } public SMSButton(Context context) { super(context); initView(); } @Override public void setOnClickListener(OnClickListener l) { super.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (!isDelayed) {// 如果没有在等待 if (clickListener != null) { if (clickListener.validate()) { setClickable(false); isDelayed = true; currentDelayTime = delayTime; getHandler().post(mTimer); clickListener.onClick(); } } } } }); } /** * 初始化页面 */ private void initView() { String text = getText().toString().toString().trim(); if (text != null && !"".equals(text)) { showText = text; } } @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); if (!mAttached) { mAttached = true; setText(showText); } } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); if (mAttached) { mAttached = false; isDelayed = false; getHandler().removeCallbacks(mTimer); } } public int getDelayTime() { return delayTime; } public void setDelayTime(int delayTime) { this.delayTime = delayTime; } public String getShowText() { return showText; } public void setShowText(String showText) { this.showText = showText; } public String getDelayText() { return delayText; } public void setDelayText(String start, String end) { this.delayText = start + FORMATETAG + end; } public SMSOnClickListener getSMSOnClickListener() { return clickListener; } public void setSMSOnClickListener(SMSOnClickListener clickListener) { setOnClickListener(null); this.clickListener = clickListener; } public interface SMSOnClickListener { public boolean validate(); public void onClick(); }</span>用法:
/** * 例子 * * smsButton = (SMSButton) findViewById(R.id.smsButton); * smsButton.setShowText("获取"); * smsButton.setDelayText("等", "秒"); smsButton.setSMSOnClickListener(new * SMSOnClickListener() { * * @Override public boolean validate() { return true; } * @Override public void onClick() { * ToastUtil.getInstance().toastInCenter(LoadingActivity.this, * "onClick"); } }); */
Android 短信验证码控件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。