一、手机号转换成3-4-4格式
public String getMobileFormat(String theMobileStr){
return theMobileStr.substring(0,3) + "" + theMobileStr.substring(3,7) + "" + theMobileStr.substring(7);
}
二、手机号输入时自动添加空格形成3-4-4格式
- final EditText et_phone = (EditText) findViewById(R.id.et_phone);
- et_phone.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){
- et_phone.setText(s + " ");
- et_phone.setSelection(et_phone.getText().toString().length());
- }
- }
- }
- @Override
- public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
- int arg3) {
- }
- @Override
- public void afterTextChanged(Editable arg0) {
- }
- });