android中EditText手机号码显示的格式为xxx xxxx xxxx,查了很多资料,有的虽然格式显示对了,但是在删除部分手机号之后继续输入
手机号的格式显示的就不是要求的酱紫了,用了好长时间整理了一份代码。ps:如果需要转换成xxx-xxxx-xxxx或者xxx-xxx-xxxxx甚至
其他格式的,可以进行根据需求进行修改。
mEditTextPhoneNumber.addTextChangedListener(new TextWatcher() {
private boolean isDelete;
@Override
public void onTextChanged(CharSequence s, int cursorPosition, int before, int count) {
mEditTextPhoneNumber.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
isDelete = true;
}
return false;
}
});
UserUtils.formatPhoneNumber(s, cursorPosition, before, count, mEditTextPhoneNumber, this);
}
@Override
public void afterTextChanged(Editable arg0) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,int arg2, int arg3) {
}
});
public class UserUtils{
public static void formatPhoneNumber(CharSequence s, int cursorPosition, int before, int count,EditText mEditText,TextWatcher mTextWatcher){
if(before == 0 && count == 1){ //Entering values
String val = s.toString();
String a = "";
String b = "";
String c = "";
if (val != null && val.length() > 0) {
val = val.replace(" ", "");
if (val.length() >= 3) {
a = val.substring(0, 3);
} else if (val.length() < 3) {
a = val.substring(0, val.length());
}
if (val.length() >= 7) {
b = val.substring(3, 7);
c = val.substring(7, val.length());
} else if (val.length() > 3 && val.length() < 7) {
b = val.substring(3, val.length());
}
StringBuffer stringBuffer = new StringBuffer();
if (a != null && a.length() > 0) {
stringBuffer.append(a);
}
if (b != null && b.length() > 0) {
stringBuffer.append(" ");
stringBuffer.append(b);
}
if (c != null && c.length() > 0) {
stringBuffer.append(" ");
stringBuffer.append(c);
}
mEditText.removeTextChangedListener(mTextWatcher);
mEditText.setText(stringBuffer.toString());
if(cursorPosition == 3 || cursorPosition == 8){
cursorPosition = cursorPosition+2;
}else{
cursorPosition = cursorPosition+1;
}
if(cursorPosition <= mEditText.getText().toString().length()) {
mEditText.setSelection(cursorPosition);
}else{
mEditText.setSelection(mEditText.getText().toString().length());
}
mEditText.addTextChangedListener(mTextWatcher);
} else {
mEditText.removeTextChangedListener(mTextWatcher);
mEditText.setText("");
mEditText.addTextChangedListener(mTextWatcher);
}
}
if(before == 1 && count == 0){ //Deleting values
String val = s.toString();
String a = "";
String b = "";
String c = "";
if (val != null && val.length() > 0) {
val = val.replace(" ", "");
if(cursorPosition == 3){
val = removeCharAt(val,cursorPosition-1,s.toString().length()-1);
}else if(cursorPosition == 8){
val = removeCharAt(val,cursorPosition-2,s.toString().length()-2);
}
if (val.length() >= 3) {
a = val.substring(0, 3);
} else if (val.length() < 3) {
a = val.substring(0, val.length());
}
if (val.length() >= 7) {
b = val.substring(3, 7);
c = val.substring(7, val.length());
} else if (val.length() > 3 && val.length() < 7) {
b = val.substring(3, val.length());
}
StringBuffer stringBuffer = new StringBuffer();
if (a != null && a.length() > 0) {
stringBuffer.append(a);
}
if (b != null && b.length() > 0) {
stringBuffer.append(" ");
stringBuffer.append(b);
}
if (c != null && c.length() > 0) {
stringBuffer.append(" ");
stringBuffer.append(c);
}
mEditText.removeTextChangedListener(mTextWatcher);
mEditText.setText(stringBuffer.toString());
if(cursorPosition == 3 || cursorPosition == 8){
cursorPosition = cursorPosition-1;
}
if(cursorPosition <= mEditText.getText().toString().length()) {
mEditText.setSelection(cursorPosition);
}else{
mEditText.setSelection(mEditText.getText().toString().length());
}
mEditText.addTextChangedListener(mTextWatcher);
} else {
mEditText.removeTextChangedListener(mTextWatcher);
mEditText.setText("");
mEditText.addTextChangedListener(mTextWatcher);
}
}
}
public static String removeCharAt(String s, int pos,int length) {
String value = "";
if(length > pos){
value = s.substring(pos + 1);
}
return s.substring(0, pos)+value ;
}
}
如需要判断手机号是否合法:
/**
* 常用手机号的判断
*/
public static boolean isMobileNO(String mobiles) {
boolean flag = false;
try {
Pattern p = Pattern.compile("^[1][3,4,5,8][0-9]{9}$"); // 验证手机号
Matcher m = p.matcher(mobiles);
flag = m.matches();
} catch (Exception e) {
flag = false;
}
return flag;
}
需要注意的是,传入的手机号需要需要去掉空格,因为这里的正则表达式不包含空格
ps:字符串去空格
tirm()只是去掉首尾的空格,不包含字符串中间的其他空格
relpace(“ ”,“”) 和replaceAll(“ ”,“”)我是没看出来有什么大的区别,用这两个任何一个都可以去掉字符串中所有的空格。