Android EdictText-电话格式输入
2017-09-11 本文已影响55人
蓝库知识
主要实现131 1244 1245的电话输入格式以及控制电话号码的长度
title.setText(getString(R.string.edit_iphone));
editText.setHint(getString(R.string.lc_try_test_phone_placeholder));
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
editText.setSelection(editText.getText().toString().length());
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(13)});
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (count == 1) {
int length = s.toString().length();
if (length > 3 && length <= 8) {
if (s.charAt(3) != ' ') {
editText.setText(s.toString().substring(0, 3) + " " + s.toString().substring(3, length));
editText.setSelection(editText.getText().toString().length());
}
}
if (length > 8) {
if (s.charAt(8) != ' ') {
editText.setText(s.toString().substring(0, 8)+" "+s.toString().substring(8));
editText.setSelection(editText.getText().toString().length());
}
}
}
}
后记:菜鸟的学习笔记