Android实现输入手机号时自动添加空格

为了用户更好的体验,我们在输入手机号的时候 添加空格,方便用户查看是否输入错误,不多说代码上去

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) {
			}
		});


你可能感兴趣的:(android)