您好,登錄后才能下訂單哦!
如何在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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。