Android实现银行卡、手机号带空格格式
程序员文章站
2022-06-28 16:02:58
本文实例为大家分享了android实现银行卡、手机号带空格格式的具体代码,供大家参考,具体内容如下工具类:package com.gongzhibao.gzb.widget; import andro...
本文实例为大家分享了android实现银行卡、手机号带空格格式的具体代码,供大家参考,具体内容如下
工具类:
package com.gongzhibao.gzb.widget; import android.content.context; import android.content.res.typedarray; import android.support.v7.widget.appcompatedittext; import android.text.editable; import android.text.inputfilter; import android.text.inputtype; import android.text.textutils; import android.text.textwatcher; import android.text.method.digitskeylistener; import android.util.attributeset; import android.widget.toast; import com.gongzhibao.gzb.r; /** * 银行卡 手机 身份证输入框 2018/7/9. */ public class contentwithspaceedittext extends appcompatedittext { public static final int type_phone = 0; public static final int type_bank_card = 1; public static final int type_id_card = 2; private int start, count,before; private int contenttype; private int maxlength = 50; private string digits; public contentwithspaceedittext(context context) { this(context, null); } public contentwithspaceedittext(context context, attributeset attrs) { super(context, attrs); parseattributeset(context, attrs); } public contentwithspaceedittext(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); parseattributeset(context, attrs); } private void parseattributeset(context context, attributeset attrs) { typedarray ta = context.obtainstyledattributes(attrs, r.styleable.contentwithspaceedittext, 0, 0); contenttype = ta.getint(r.styleable.contentwithspaceedittext_input_type, 0); // 必须调用recycle ta.recycle(); inittype(); setsingleline(); addtextchangedlistener(watcher); } private void inittype(){ if (contenttype == type_phone) { maxlength = 13; digits = "0123456789 "; setinputtype(inputtype.type_class_number); } else if (contenttype == type_bank_card) { maxlength = 31; digits = "0123456789 "; setinputtype(inputtype.type_class_number); } else if (contenttype == type_id_card) { maxlength = 21; digits = null; setinputtype(inputtype.type_class_text); } setfilters(new inputfilter[]{new inputfilter.lengthfilter(maxlength)}); } @override public void setinputtype(int type) { if (contenttype == type_phone || contenttype == type_bank_card) { type = inputtype.type_class_number; }else if(contenttype == type_id_card){ type = inputtype.type_class_text; } super.setinputtype(type); /* 非常重要:setkeylistener要在setinputtype后面调用,否则无效。*/ if(!textutils.isempty(digits)) { setkeylistener(digitskeylistener.getinstance(digits)); } } /** * 设置内容的类型 * @param contenttype 类型 */ public void setcontenttype(int contenttype) { this.contenttype = contenttype; inittype(); } private textwatcher watcher = new textwatcher() { @override public void beforetextchanged(charsequence s, int start, int count, int after) { } @override public void ontextchanged(charsequence s, int start, int before, int count) { contentwithspaceedittext.this.start = start; contentwithspaceedittext.this.before = before; contentwithspaceedittext.this.count = count; } @override public void aftertextchanged(editable s) { if (s == null) { return; } //判断是否是在中间输入,需要重新计算 boolean ismiddle = (start + count) < (s.length()); //在末尾输入时,是否需要加入空格 boolean isneedspace = false; if (!ismiddle && isspace(s.length())) { isneedspace = true; } if (ismiddle || isneedspace || count > 1) { string newstr = s.tostring(); newstr = newstr.replace(" ", ""); stringbuilder sb = new stringbuilder(); int spacecount = 0; for (int i = 0; i < newstr.length(); i++) { sb.append(newstr.substring(i, i+1)); //如果当前输入的字符下一位为空格(i+1+1+spacecount),因为i是从0开始计算的,所以一开始的时候需要先加1 if(isspace(i + 2 + spacecount)){ sb.append(" "); spacecount += 1; } } removetextchangedlistener(watcher); s.replace(0, s.length(),sb); //如果是在末尾的话,或者加入的字符个数大于零的话(输入或者粘贴) if (!ismiddle || count > 1) { setselection(s.length() <= maxlength ? s.length() : maxlength); } else { //如果是删除 if (count == 0) { //如果删除时,光标停留在空格的前面,光标则要往前移一位 if (isspace(start - before + 1)) { setselection((start - before) > 0 ? start - before : 0); } else { setselection((start - before + 1) > s.length() ? s.length() : (start - before + 1)); } } //如果是增加 else { if (isspace(start - before + count)) { setselection((start + count - before + 1) < s.length() ? (start + count - before + 1) : s.length()); } else { setselection(start + count - before); } } } addtextchangedlistener(watcher); } } }; public string gettextwithoutspace() { return super.gettext().tostring().replace(" ", ""); } public boolean checktextright(){ string text = gettextwithoutspace(); //这里做个简单的内容判断 if (contenttype == type_phone) { if (textutils.isempty(text)) { showshort(getcontext(), "手机号不能为空,请输入正确的手机号"); } else if (text.length() < 11) { showshort(getcontext(), "手机号不足11位,请输入正确的手机号"); } else { return true; } } else if (contenttype == type_bank_card) { if (textutils.isempty(text)) { showshort(getcontext(), "银行卡号不能为空,请输入正确的银行卡号"); } else if (text.length() < 14) { showshort(getcontext(), "银行卡号位数不正确,请输入正确的银行卡号"); } else { return true; } } else if (contenttype == type_id_card) { if (textutils.isempty(text)) { showshort(getcontext(), "身份证号不能为空,请输入正确的身份证号"); } else if (text.length() < 18) { showshort(getcontext(), "身份证号不正确,请输入正确的身份证号"); } else { return true; } } return false; } private void showshort(context context,string msg){ toast.maketext(context.getapplicationcontext(), msg, toast.length_short).show(); } private boolean isspace(int length) { if (contenttype == type_phone) { return isspacephone(length); } else if (contenttype == type_bank_card) { return isspacecard(length); } else if (contenttype == type_id_card) { return isspaceidcard(length); } return false; } private boolean isspacephone(int length) { return length >= 4 && (length == 4 || (length + 1) % 5 == 0); } private boolean isspacecard(int length) { return length % 5 == 0; } private boolean isspaceidcard(int length) { return length > 6 && (length == 7 || (length - 2) % 5 == 0); } }
资源文件values下创建attrs文件
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="contentwithspaceedittext"> <attr name="input_type" format="enum"> <enum name="phone" value="0"/> <enum name="bank_card_no" value="1"/> <enum name="identify_card_no" value="2"/> </attr> </declare-styleable> </resources>
使用
bank.setcontenttype(1);//银行卡号 phone.setcontenttype(0);//电话号 number.setcontenttype(2);//身份证号
获取内容(去掉空格)
phone.gettext().tostring().replace(" ", "");
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 2.3.1 物理层设备
下一篇: JS实现前端缓存的方法