APP中注册时常用的发送验证码的Button,带倒计时重发功能
来源:互联网 发布:phpcms建站 编辑:程序博客网 时间:2024/06/05 07:32
finddreams:http://blog.csdn.net/finddreams/article/details/44174775
注册时我们经常会碰到,给手机发送验证码的功能,点击发送验证码,然后就是显示剩余多少秒之后重新发送验证码,效果图如下:
为了实现这样的效果,当用户点击发送验证码时,显示为剩余多少秒重新发送,同时设置这个Button的状态为不可点击,所以是个灰色背景。等倒计时完了之后,把Button的状态置为可以点击状态。Android中内置的普通的Button是达不到要求的,我们需要拓展,所以需要自定义一个Button,代码如下:
/** * @Description:发送验证码的button,带有倒计时,以及在发送的过程中不可点击; * 调用方式 view.startTickWork()方法即可; * @author http://blog.csdn.net/finddreams */public class SendValidateButton extends Button { private static final int DISABLE_TIME = 60; private static final int MSG_TICK = 0; private Timer mTimer = null; private TimerTask mTask = null; private int mDisableTime = DISABLE_TIME; // 倒计时时间,默认60秒 private int mEnableColor = Color.WHITE; private int mDisableColor = Color.GRAY; private String mEnableString = "获取验证码"; private String mDisableString = "剩余"; private String Second = "秒"; private boolean mClickBle = true; private SendValidateButtonListener mListener = null; public int getmDisableTime() { return mDisableTime; } public int getmEnableColor() { return mEnableColor; } public void setmEnableColor(int mEnableColor) { this.mEnableColor = mEnableColor; this.setTextColor(mEnableColor); } public int getmDisableColor() { return mDisableColor; } public void setmDisableColor(int mDisableColor) { this.mDisableColor = mDisableColor; } public String getmEnableString() { return mEnableString; } public boolean isDisable() { if (mDisableTime > 0) { return true; } return false; } public void setmEnableString(String mEnableString) { this.mEnableString = mEnableString; if (this.mEnableString != null) { this.setText(mEnableString); } } public String getmDisableString() { return mDisableString; } public void setmDisableString(String mDisableString) { this.mDisableString = mDisableString; } public void setmListener(SendValidateButtonListener mListener) { this.mListener = mListener; } private Handler mHandler = new Handler(Looper.getMainLooper()) { @Override public void handleMessage(Message msg) { switch (msg.what) { case MSG_TICK: tickWork(); break; default: break; } super.handleMessage(msg); } }; public SendValidateButton(Context context, AttributeSet attrs) { super(context, attrs); initView(); } private void initView() { this.setText(mEnableString); this.setGravity(Gravity.CENTER); this.setTextColor(mEnableColor); initTimer(); this.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mListener != null && mClickBle) { // startTickWork(); mListener.onClickSendValidateButton(); } } }); } private void initTimer() { mTimer = new Timer(); } private void initTimerTask() { mTask = new TimerTask() { @Override public void run() { mHandler.sendEmptyMessage(MSG_TICK); } }; } public void startTickWork() { if (mClickBle) { mClickBle = false; SendValidateButton.this.setText(mDisableString + mDisableTime + Second); this.setEnabled(false); SendValidateButton.this.setTextColor(mDisableColor); initTimerTask(); mTimer.schedule(mTask, 0, 1000); } } /** * 每秒钟调用一次 */ private void tickWork() { mDisableTime--; this.setText(mDisableString + mDisableTime + Second); if (mListener != null) { mListener.onTick(); } if (mDisableTime <= 0) { stopTickWork(); } } public void stopTickWork() { mTask.cancel(); mTask = null; mDisableTime = DISABLE_TIME; this.setText(mEnableString); this.setTextColor(mEnableColor); this.setEnabled(true); mClickBle = true; } public interface SendValidateButtonListener { public void onClickSendValidateButton(); public void onTick(); }}
然后我们只需要在用到地方引用一下即可:
<com.finddreams.view.SendValidateButton android:id="@id/getcode_btn" android:textColor="@color/white" android:layout_width="wrap_content" android:layout_height="match_parent" android:padding="5dip" android:background="@drawable/bg_btn_shape_login" android:text="发送验证码" android:textSize="16sp" />
我们来看一下自定义Button的background的选择器bg_btn_shape_login.xml是如何做的:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"><shape android:shape="rectangle"> <stroke android:width="1.0px" android:color="#ffd3dde6" /> <corners android:bottomLeftRadius="5.0dip" android:bottomRightRadius="5.0dip" android:topLeftRadius="5.0dip" android:topRightRadius="5.0dip" /> <gradient android:angle="270.0" android:endColor="#ff0e9379" android:startColor="#ff0e9379" /> </shape></item> <item android:state_focused="true"><shape android:shape="rectangle"> <gradient android:angle="270.0" android:endColor="#ff0e9379" android:startColor="#ff0e9379" /> <stroke android:width="1.0px" android:color="#ffd3dde6" /> <corners android:bottomLeftRadius="5.0dip" android:bottomRightRadius="5.0dip" android:topLeftRadius="5.0dip" android:topRightRadius="5.0dip" /> </shape></item> <item android:state_enabled="false"><shape android:shape="rectangle"> <gradient android:angle="270.0" android:endColor="#ffe8ecef" android:startColor="#ffe8ecef" /> <stroke android:width="1.0px" android:color="#ffd3dde6" /> <corners android:bottomLeftRadius="5.0dip" android:bottomRightRadius="5.0dip" android:topLeftRadius="5.0dip" android:topRightRadius="5.0dip" /> </shape></item> <item> <shape android:shape="rectangle"> <gradient android:angle="270.0" android:endColor="#2B86E3" android:startColor="#2B86E3" /> <stroke android:width="1.0px" android:color="#ffd3dde6" /> <corners android:bottomLeftRadius="5.0dip" android:bottomRightRadius="5.0dip" android:topLeftRadius="5.0dip" android:topRightRadius="5.0dip" /> </shape> </item></selector>
这样就可以实现发送验证码的Button功能了,是不是很简单?当然应该还是有其他实现形式的,大家有兴趣的话可以自己去琢磨一下。
9 0
- APP中注册时常用的发送验证码的Button,带倒计时重发功能
- APP中注册时常用的发送验证码的Button,带倒计时重发功能
- 实现 发送验证码的倒计时功能
- iOS发送验证码倒计时功能的实现
- 发送验证码倒计时功能
- Android中注册、找回密码等发送验证码倒计时
- button倒计时,发送手机验证码可用
- 简单的自定义view,实现倒计时发送验证码的button
- 带有倒计时功能的button
- 注册帐号时发送验证码倒计时
- Android发送验证码的倒计时按钮
- Jquery简单的发送验证码倒计时
- 发送验证码的倒计时。GCD方法
- js实现发送短信验证码后的倒计时功能(无视页面刷新)
- js实现发送短信验证码后的倒计时功能(无视页面刷新)
- js实现发送短信验证码后的倒计时功能(无视页面刷新)
- Android 验证码倒计时功能的实现
- android 带倒计时效果的Button
- 黑马程序员-Java学习笔记之异常和包
- linux yum 安装svn
- 死锁的必要条件和解决方法
- leetCode #191 number of 1 bits
- 开篇记(2015.03.10)
- APP中注册时常用的发送验证码的Button,带倒计时重发功能
- LKT4200HS 32位高性能防盗版加密芯片
- hdu 1561 the more the better (树形DP)
- java 实现99乘法表
- 日本とドイツ―国際秩序強化へ協働を
- 浅谈android4.0开发之GridLayout布局
- CentOS下github生成公钥
- 20150310 N1
- 音频、语音信号处理的相关需求(广告)