Android EditText實(shí)現(xiàn)分割輸入內(nèi)容
在項(xiàng)目中可能會(huì)有許多需要輸入手機(jī)號(hào)碼、銀行卡號(hào)或者身份證號(hào)等內(nèi)容的輸入框。如果直接輸入的話將會(huì)是一堆號(hào)碼堆在一起,第一是不太美觀,第二也容易出錯(cuò),用戶體驗(yàn)不太好。但是若將輸入的號(hào)碼按特定格式進(jìn)行分割將會(huì)大大提高用戶體驗(yàn)!
以下是對(duì)常用的號(hào)碼進(jìn)行簡(jiǎn)單封裝的自定義輸入框控件,方便我們?cè)陂_發(fā)過程中使用:
- 該控件支持xml屬性指定,也支持代碼指定;
- 該控件支持類型分別為電話號(hào)碼(000 0000 0000)、銀行卡號(hào)(0000 0000 0000 0000 000)和身份證號(hào)(000000 0000 0000 0000)。
效果圖
自定義EditText
/** * @Description 分割輸入框 * @Author 一花一世界 */ public class ContentWithSpaceEditText extends EditText { public static final int TYPE_PHONE = 0; public static final int TYPE_CARD = 1; public static final int TYPE_IDCARD = 2; private int maxLength = 100; private int contentType; private int start, count, before; 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_type, 0); 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_CARD) { maxLength = 23; digits = "0123456789 "; setInputType(InputType.TYPE_CLASS_NUMBER); } else if (contentType == TYPE_IDCARD) { maxLength = 21; digits = "0123456789xX "; setInputType(InputType.TYPE_CLASS_TEXT); } setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)}); } @Override public void setInputType(int type) { super.setInputType(type); // setKeyListener要在setInputType后面調(diào)用,否則無效 if (!TextUtils.isEmpty(digits)) { setKeyListener(DigitsKeyListener.getInstance(digits)); } } 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; } //判斷是否是在中間輸入,需要重新計(jì)算 boolean isMiddle = (start + count) < (s.length()); //在末尾輸入時(shí),是否需要加入空格 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)); //如果當(dāng)前輸入的字符下一位為空格(i+1+1+spaceCount),因?yàn)閕是從0開始計(jì)算的,所以一開始的時(shí)候需要先加1 if (isSpace(i + 2 + spaceCount)) { sb.append(" "); spaceCount += 1; } } removeTextChangedListener(watcher); s.replace(0, s.length(), sb); //如果是在末尾的話,或者加入的字符個(gè)數(shù)大于零的話(輸入或者粘貼) if (!isMiddle || count > 1) { setSelection(s.length() <= maxLength ? s.length() : maxLength); } else if (isMiddle) { //如果是刪除 if (count == 0) { //如果刪除時(shí),光標(biāo)停留在空格的前面,光標(biāo)則要往前移一位 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); } } }; private boolean isSpace(int length) { if (contentType == TYPE_PHONE) { return isSpacePhone(length); } else if (contentType == TYPE_CARD) { return isSpaceCard(length); } else if (contentType == TYPE_IDCARD) { 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); } public void setContentType(int contentType) { this.contentType = contentType; initType(); } public String getTextWithoutSpace() { return super.getText().toString().replace(" ", ""); } /** * @Description 內(nèi)容校驗(yàn) */ public boolean isContentCheck() { String text = getTextWithoutSpace(); if (contentType == TYPE_PHONE) { if (TextUtils.isEmpty(text)) { ToastUtil.showText(UIUtils.getString(R.string.phone_number_not_empty)); } else if (text.length() < 11) { ToastUtil.showText(UIUtils.getString(R.string.phone_number_incorrect_length)); } else { return true; } } else if (contentType == TYPE_CARD) { if (TextUtils.isEmpty(text)) { ToastUtil.showText(UIUtils.getString(R.string.bank_card_not_empty)); } else if (text.length() < 16) { ToastUtil.showText(UIUtils.getString(R.string.bank_card_incorrect_length)); } else { return true; } } else if (contentType == TYPE_IDCARD) { if (TextUtils.isEmpty(text)) { ToastUtil.showText(UIUtils.getString(R.string.identity_number_not_empty)); } else if (text.length() < 18) { ToastUtil.showText(UIUtils.getString(R.string.identity_number_incorrect_length)); } else { return true; } } return false; } }
配置attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="ContentWithSpaceEditText"> <attr name="type" format="enum"> <enum name="phone" value="0" /> <enum name="card" value="1" /> <enum name="IDCard" value="2" /> </attr> </declare-styleable> </resources>
布局文件中使用
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/theme_bg" android:orientation="vertical"> <com.wiggins.splitinput.widget.TitleView android:id="@+id/titleView" android:layout_width="match_parent" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="match_parent" android:layout_height="@dimen/item_normal" android:layout_margin="@dimen/margin_normal" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="@dimen/btn_width_normal" android:layout_height="match_parent" android:gravity="center_vertical" android:text="@string/phone_number" android:textColor="@color/blue" android:textSize="@dimen/font_normal" /> <com.wiggins.splitinput.widget.ContentWithSpaceEditText android:id="@+id/edt_phone_input" android:layout_width="match_parent" android:layout_height="@dimen/item_normal" android:background="@color/white" android:gravity="center" android:hint="@string/please_enter_content" android:inputType="number" android:textColor="@color/blue" android:textColorHint="@color/gray" android:textCursorDrawable="@null" android:textSize="@dimen/font_normal" app:type="phone" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="@dimen/item_normal" android:layout_margin="@dimen/margin_normal" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="@dimen/btn_width_normal" android:layout_height="match_parent" android:gravity="center_vertical" android:text="@string/bank_card_number" android:textColor="@color/blue" android:textSize="@dimen/font_normal" /> <com.wiggins.splitinput.widget.ContentWithSpaceEditText android:id="@+id/edt_bank_card_input" android:layout_width="match_parent" android:layout_height="@dimen/item_normal" android:background="@color/white" android:gravity="center" android:hint="@string/please_enter_content" android:inputType="number" android:textColor="@color/blue" android:textColorHint="@color/gray" android:textCursorDrawable="@null" android:textSize="@dimen/font_normal" app:type="card" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="@dimen/item_normal" android:layout_margin="@dimen/margin_normal" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="@dimen/btn_width_normal" android:layout_height="match_parent" android:gravity="center_vertical" android:text="@string/identity_number" android:textColor="@color/blue" android:textSize="@dimen/font_normal" /> <com.wiggins.splitinput.widget.ContentWithSpaceEditText android:id="@+id/edt_identity_input" android:layout_width="match_parent" android:layout_height="@dimen/item_normal" android:background="@color/white" android:gravity="center" android:hint="@string/please_enter_content" android:inputType="number" android:textColor="@color/blue" android:textColorHint="@color/gray" android:textCursorDrawable="@null" android:textSize="@dimen/font_normal" app:type="IDCard" /> </LinearLayout> </LinearLayout>
項(xiàng)目地址:傳送門
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 開發(fā)調(diào)試工具的使用總結(jié)
這篇文章主要介紹了Android 開發(fā)調(diào)試工具的使用總結(jié)的相關(guān)資料,這里整理了,Android開發(fā)所需要的常用工具,需要的朋友可以參考下2016-11-11Android判斷json格式將錯(cuò)誤信息提交給服務(wù)器
今天小編就為大家分享一篇關(guān)于Android判斷json格式將錯(cuò)誤信息提交給服務(wù)器,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03模仿美團(tuán)點(diǎn)評(píng)的Android應(yīng)用中價(jià)格和購買欄懸浮固定的效果
這篇文章主要介紹了模仿美團(tuán)點(diǎn)評(píng)的Android應(yīng)用中價(jià)格和購買欄懸浮固定的效果,文章后半還針對(duì)快速滑動(dòng)操作給出了一個(gè)響應(yīng)優(yōu)化的方法,需要的朋友可以參考下2016-04-04Android 獲取正在運(yùn)行的任務(wù)和服務(wù)的小例子
Android 獲取正在運(yùn)行的任務(wù)和服務(wù)的小例子,需要的朋友可以參考一下2013-05-05Android客戶端與服務(wù)端數(shù)據(jù)加密傳輸方案詳解
這篇文章主要為大家介紹了Android客戶端與服務(wù)端數(shù)據(jù)加密傳輸方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Android開發(fā)框架MVC-MVP-MVVM-MVI的演變Demo
這篇文章主要為大家介紹了Android開發(fā)框架MVC-MVP-MVVM-MVI的演變Demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10Android創(chuàng)建淡入淡出動(dòng)畫的詳解
大家好,本篇文章主要講的是Android創(chuàng)建淡入淡出動(dòng)畫的詳解,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12Android音頻錄制MediaRecorder之簡(jiǎn)易的錄音軟件實(shí)現(xiàn)代碼
這篇文章主要介紹了Android音頻錄制MediaRecorder之簡(jiǎn)易的錄音軟件實(shí)現(xiàn)代碼,有需要的朋友可以參考一下2014-01-01