在项目中可能会有许多需要输入手机号码、yhk号或者身份z号等内容的输入框。如果直接输入的话将会是一堆号码堆在一起,第一是不太美观,第二也容易出错,用户体验不太好。但是若将输入的号码按特定格式进行分割将会大大提高用户体验!
以下是对常用的号码进行简单封装的自定义输入框控件,方便我们在开发过程中使用:
该控件支持xml属性指定,也支持代码指定; 该控件支持类型分别为电话号码(000 0000 0000)、yhk号(0000 0000 0000 0000 000)和身份z号(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); 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后面调用,否则无效 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 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 (isMIDdle) { //如果是删除 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); } } }; 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 内容校验 */ 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>
项目地址:传送门
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android EditText实现分割输入内容全部内容,希望文章能够帮你解决Android EditText实现分割输入内容所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)