如何在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(" ", "");
看完上述内容,你们掌握如何在Android中实现一个银行卡、手机号格式的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。