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"); } });
	 */



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。