在我的应用程序中,我有一个 editText 它将接受用户的电话号码,我的目标是,一旦用户输入电话号码,它就应该被格式化(就像通过应用文本更改的监听器一样),格式就像XXX-XXX-XXXX
.
我把代码写成
ePhone.addTextChangedListener(new TextWatcher() {
private Pattern pattern;
private Matcher matcher;
String a;
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
boolean flag = true;
if (flag) {
if (((ePhone.getText().length() + 1) % 4) == 0)
{
if (ePhone.getText().toString().split("-").length <= 2)
{
ePhone.setText(ePhone.getText() + "-");
ePhone.setSelection(ePhone.getText().length());
}
}
a = ePhone.getText().toString();
} else {
ePhone.setText(a);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stus
}
});
但是当用户想从中删除一个号码时editText
,它就无法正常工作。提前致谢。