手机号添加空格形成(3-4-4)格式

一、手机号转换成3-4-4格式

public String getMobileFormat(String theMobileStr){

     return theMobileStr.substring(0,3) + "" + theMobileStr.substring(3,7) + "" + theMobileStr.substring(7);

}

二、手机号输入时自动添加空格形成3-4-4格式

  1. final EditText et_phone = (EditText) findViewById(R.id.et_phone);  
  2.         et_phone.addTextChangedListener(new TextWatcher() {  
  3.             @Override  
  4.             public void onTextChanged(CharSequence s, int start, int before, int count) {  
  5.                 if (count == 1){  
  6.                     int length = s.toString().length();  
  7.                     if (length == 3 || length == 8){  
  8.                         et_phone.setText(s + " ");  
  9.                         et_phone.setSelection(et_phone.getText().toString().length());  
  10.                     }  
  11.                 }  
  12.             }  
  13.             @Override  
  14.             public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,  
  15.                     int arg3) {  
  16.                   
  17.             }  
  18.             @Override  
  19.             public void afterTextChanged(Editable arg0) {  
  20.             }  
  21.         }); 

猜你喜欢

转载自blog.csdn.net/qiy6010/article/details/80191630