手机号自动补空格形成(3-4-4)格式

private EditText editText;

在TextWatch类中的类中的onTextChanged方法执行

public void onTextChanged(CharSequence s, int start, int before, int count) {
if (count == 1) {
if (s.length() == 4) {
editText.setText(s.subSequence(0, s.length() - 1) + ” ”
+ s.subSequence(s.length() - 1, s.length()));
editText.setSelection(5);
}
if (s.length() == 9) {
editText.setText(s.subSequence(0, s.length() - 1) + ” ”
+ s.subSequence(s.length() - 1, s.length()));
editText.setSelection(10);
}

        } else if (count == 0) {
            if (s.length() == 4) {
                editText.setText(s.subSequence(0, s.length() - 1));
                editText.setSelection(3);
            }
            if (s.length() == 9) {
               editText.setText(s.subSequence(0, s.length() - 1));
               editText.setSelection(8);
            }

        }
    }

你可能感兴趣的:(手机号自动补空格形成(3-4-4)格式)