请重新发个微拍发送验证码时

APP中登记时常用的发送验证码的Button,带倒计时重发功能 - 移动开发当前位置:& &&&APP中登记时常用的发送验证码的Button,带倒计时重APP中登记时常用的发送验证码的Button,带倒计时重发功能&&网友分享于:&&浏览:0次APP中注册时常用的发送验证码的Button,带倒计时重发功能finddreams:http://blog.csdn.net/finddreams/article/details/
注册时我们经常会碰到,给手机发送验证码的功能,点击发送验证码,然后就是显示剩余多少秒之后重新发送验证码,效果图如下:
为了实现这样的效果,当用户点击发送验证码时,显示为剩余多少秒重新发送,同时设置这个Button的状态为不可点击,所以是个灰色背景。等倒计时完了之后,把Button的状态置为可以点击状态。Android中内置的普通的Button是达不到要求的,我们需要拓展,所以需要自定义一个Button,代码如下:
*:发送验证码的button,带有倒计时,以及在发送的过程中不可点击;
* 调用方式 view.startTickWork()方法即可;
* 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;
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 mDisableT
public int getmEnableColor() {
return mEnableC
public void setmEnableColor(int mEnableColor) {
this.mEnableColor = mEnableC
this.setTextColor(mEnableColor);
public int getmDisableColor() {
return mDisableC
public void setmDisableColor(int mDisableColor) {
this.mDisableColor = mDisableC
public String getmEnableString() {
return mEnableS
public boolean isDisable() {
if (mDisableTime & 0) {
return true;
return false;
public void setmEnableString(String mEnableString) {
this.mEnableString = mEnableS
if (this.mEnableString != null) {
this.setText(mEnableString);
public String getmDisableString() {
return mDisableS
public void setmDisableString(String mDisableString) {
this.mDisableString = mDisableS
public void setmListener(SendValidateButtonListener mListener) {
this.mListener = mL
private Handler mHandler = new Handler(Looper.getMainLooper()) {
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_TICK:
tickWork();
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() {
public void onClick(View v) {
if (mListener != null && mClickBle)
mListener.onClickSendValidateButton();
private void initTimer() {
mTimer = new Timer();
private void initTimerTask() {
mTask = new TimerTask()
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"?&
xmlns:android="/apk/res/android"&
android:state_pressed="true"& android:shape="rectangle"&
android:width="1.0px" android:color="#ffd3dde6" /&
android:bottomLeftRadius="5.0dip" android:bottomRightRadius="5.0dip" android:topLeftRadius="5.0dip" android:topRightRadius="5.0dip" /&
android:angle="270.0" android:endColor="#ff0e9379" android:startColor="#ff0e9379" /&
android:state_focused="true"& android:shape="rectangle"&
android:angle="270.0" android:endColor="#ff0e9379" android:startColor="#ff0e9379" /&
android:width="1.0px" android:color="#ffd3dde6" /&
android:bottomLeftRadius="5.0dip" android:bottomRightRadius="5.0dip" android:topLeftRadius="5.0dip" android:topRightRadius="5.0dip" /&
android:state_enabled="false"& android:shape="rectangle"&
android:angle="270.0" android:endColor="#ffe8ecef" android:startColor="#ffe8ecef" /&
android:width="1.0px" android:color="#ffd3dde6" /&
android:bottomLeftRadius="5.0dip" android:bottomRightRadius="5.0dip" android:topLeftRadius="5.0dip" android:topRightRadius="5.0dip" /&
android:shape="rectangle"&
android:angle="270.0" android:endColor="#2B86E3" android:startColor="#2B86E3" /&
android:width="1.0px" android:color="#ffd3dde6" /&
android:bottomLeftRadius="5.0dip" android:bottomRightRadius="5.0dip" android:topLeftRadius="5.0dip" android:topRightRadius="5.0dip" /&
这样就可以实现发送验证码的Button功能了,是不是很简单?当然应该还是有其他实现形式的,大家有兴趣的话可以自己去琢磨一下。
12345678910
12345678910
12345678910 上一篇:下一篇:文章评论相关解决方案 1234567891011 Copyright & &&版权所有您的举报已经提交成功,我们将尽快处理,谢谢!
?(前提:不重新申请帐号)当然不用!^_^ 请从新申请邮箱 去帐户管理 更改邮箱
去重新申请一个吧,貌似申请的人太多,可能有重复的 嘎嘎。
大家还关注
<a href="/b/44328.html" target="_blank" title="我登入天堂2出现............. 我登入天堂2出现如下提示:OS: Windows XP 5.1 (Build: 2600)
CPU: GenuineIntel Unknown processor @ 2809 MHz with 511MB RAM
Video: NVIDIA GeForce4 MX )
General protection fault!
History: CAuthSocket::OnUserNetmessage <- UWindowsViewport::ViewportWndProc <- WWindow::StaticProc <- MessagePump 我登入天堂2出现...............
<a href="/b/44341.html" target="_blank" title="我下载客户端已经3次了,怎么还进不去? OS:Windows20005.0( ) :NVIDIAGeForceFX5600SE(OmegaKX1.4523a)(4523)Can'tfindfileforpackage'LineageWarriors' ::LoadDefaultCharacterInfo<-UGameEngine::Init我下载客户端已经3次了,怎么还进不去? ...
<a href="/b/44493.html" target="_blank" title="我下载的客户端安装好怎么进不去是怎么回事呢? OS:Windows20005.0( ) :NVIDIAGeForceFX5600SE(OmegaKX1.4523a)(4523)Can'tfindfileforpackage'LineageWarriors' ::LoadDefaultCharacterInfo<-UGameEngine::Init我下载的客户端安装好怎么进不去是怎么回事...
(window.slotbydup=window.slotbydup || []).push({
id: '2081942',
container: s,
size: '1000,60',
display: 'inlay-fix'}

我要回帖

更多关于 请点击验证码完成发贴 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信