自动格式手机号、银行卡的EditText
2019-03-14 本文已影响0人
大_川
依照产品需求当用户输入手机号,银行卡是能自动格式化(添加分隔符),方便用户输入、阅读;同时手机号输入不能超过11位,银行卡不超过19位;
依照产品的需求定制一个简单的EditText,基于系统EditText 扩展一个自己的控件;
先上最后的效果,图片太大加载不出来,压缩了一下有点丢帧:
edittext.gif
- 既然是基于EditText 扩展,那就直接继承系统控件,同时要支持输入手机号、银行卡,那就需要自定义输入框的类型,这里自定义枚举类型
<declare-styleable name="PatternEditText">
<attr name="Format_Type" format="enum">
<enum name="mobile_phone" value="1"/>
<enum name="bank_card" value="2"/>
</attr>
</declare-styleable>
那么在控件初始化的时候读取XML中配置的属性,就可以更加不同的类型,在不同的位置分隔;
企业微信截图_15525508201553.png
public class PatternEditText extends android.support.v7.widget.AppCompatEditText {
public PatternEditText(Context context) {
this(context, null);
}
public PatternEditText(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public PatternEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PatternEditText, defStyleAttr, 0);
inputType = typedArray.getString(R.styleable.PatternEditText_Format_Type);//读取配置的类型
typedArray.recycle();
init();
}
}
2.初始化一些基本的属性,设置只可以输入数字
private void init() {
shouldStopChange = false;
setInputType(InputType.TYPE_CLASS_NUMBER);
setFocusable(true);
setEnabled(true);
setFocusableInTouchMode(true);
setCursorVisible(true);
addTextChangedListener(watcher);
}
3.格式化(插入分隔符)可以在用户输入后,可以对改控件添加addTextChangedListener监听,在afterTextChanged中处理,逻辑很简单,直接贴代码了:
private TextWatcher watcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
format();
}
};
private void format() {
if (!TextUtils.isEmpty(getText().toString())){
String value = getText().toString();
if (!TextUtils.isEmpty(value) && value.length() > inputMaxLength){
value = value.substring(0, inputMaxLength);
}
if (shouldStopChange) {
shouldStopChange = false;
return;
}
shouldStopChange = true;
StringBuffer inputValue = new StringBuffer(value);
int selectionStart = getSelectionStart();
for (int i = 0; i < inputValue.length(); i++) {
char charAt = inputValue.charAt(i);
if (charAt == WHITE_SPACE) {
inputValue.delete(i, i + 1);
if (i < selectionStart) {
selectionStart--;
}
i--;
} else if ((i-fristWhiteSpacePosition) % white_space_step == 0) {
inputValue.insert(i, WHITE_SPACE);
if (i < selectionStart) {
selectionStart++;
}
}
}
setText(inputValue);
setSelection(Math.min(inputValue.length(), selectionStart));
}
}
逻辑比较简单,总共就100行代码,统一整理如下:
import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import com.dachuan.R;
/**
* Created by admin on 2019/3/13.
*/
public class PatternEditText extends android.support.v7.widget.AppCompatEditText {
private String inputType = "1";//默认手机格式
private static final char WHITE_SPACE = ' ';//空格
private boolean shouldStopChange = false;
private int inputMaxLength;
private int phoneLength = 11;//手机号码长度
private int phoneFristSpacePosition = 3;//手机号第一个空格位置
private int bankNumLength = 19;//银行卡最大长度
private int bankNumFristSpacePosition = 4;//银行卡第一个空格位置
private int white_space_step = 5;//空格间隔
private int fristWhiteSpacePosition;
public PatternEditText(Context context) {
this(context, null);
}
public PatternEditText(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public PatternEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PatternEditText, defStyleAttr, 0);
inputType = typedArray.getString(R.styleable.PatternEditText_Format_Type);
typedArray.recycle();
init();
}
private void init() {
shouldStopChange = false;
setInputType(InputType.TYPE_CLASS_NUMBER);
setFocusable(true);
setEnabled(true);
setFocusableInTouchMode(true);
setCursorVisible(true);
addTextChangedListener(watcher);
initInputType();
format();
}
private void initInputType() {
if ("1".equals(inputType)){
inputMaxLength = phoneLength + 2;
fristWhiteSpacePosition = phoneFristSpacePosition;
}else {
inputMaxLength = bankNumLength + 4;
fristWhiteSpacePosition = bankNumFristSpacePosition;
}
}
private TextWatcher watcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
format();
}
};
private void format() {
if (!TextUtils.isEmpty(getText().toString())){
String value = getText().toString();
if (!TextUtils.isEmpty(value) && value.length() > inputMaxLength){
value = value.substring(0, inputMaxLength);
}
if (shouldStopChange) {
shouldStopChange = false;
return;
}
shouldStopChange = true;
StringBuffer inputValue = new StringBuffer(value);
int selectionStart = getSelectionStart();
for (int i = 0; i < inputValue.length(); i++) {
char charAt = inputValue.charAt(i);
if (charAt == WHITE_SPACE) {
inputValue.delete(i, i + 1);
if (i < selectionStart) {
selectionStart--;
}
i--;
} else if ((i-fristWhiteSpacePosition) % white_space_step == 0) {
inputValue.insert(i, WHITE_SPACE);
if (i < selectionStart) {
selectionStart++;
}
}
}
setText(inputValue);
setSelection(Math.min(inputValue.length(), selectionStart));
}
}
}