EditText 输入自动带空格的手机号码

在android开发过程中,经常会要求用户输入手机号,为了便于观看,我们都会以150 xxxx xxxx这种格式展示。
###1.效果图
EditText 输入自动带空格的手机号码_第1张图片
###2.布局文件




    

    

###3.代码

public class MainActivity extends AppCompatActivity {
    private EditText mPhoneNum;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        initView();

        initData();

    }
	private void initView() {
        mPhoneNum = (EditText)findViewById(R.id.editText);
    }
    private void initData() {
        mPhoneNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                if (charSequence == null || charSequence.length() == 0) {
                    return;
                }
                StringBuilder stringBuilder = new StringBuilder();
                for (int i = 0;i

你可能感兴趣的:(Android微技巧)