我搜索了所有与我的问题类似的问题,但都没有解决。我的问题是:
我想在输入时在EditText中格式化一个字符串。 格式如下(它始终是19位数字):
012345 01 0123456789 0
正如您所看到的,我希望在用户输入时根据需要添加空格。 我知道我必须使用TextWatcher,但无论我做什么,都无法得到我想要的结果。
编辑:
这是我最后一次尝试的代码:
我想在输入时在EditText中格式化一个字符串。 格式如下(它始终是19位数字):
012345 01 0123456789 0
正如您所看到的,我希望在用户输入时根据需要添加空格。 我知道我必须使用TextWatcher,但无论我做什么,都无法得到我想要的结果。
编辑:
这是我最后一次尝试的代码:
@Override
public void afterTextChanged(Editable s) {
if(s.length() == 7 || s.length() == 10 || s.length() == 21){
editText.removeTextChangedListener(this);
String newValue;
newValue= s.insert((s.length()-1), " ").toString();
//Log.d("AFTER",newValue);
editText.setText(newValue);
editText.setSelection(newValue.length());
editText.addTextChangedListener(this);
}
}