每次用户键入新字符时,我都想编辑EditText字段内容.基本上我想用libphonenumber
格式化电话号码.
我实现了一个TextWatcher,它读取字段内容并将其格式化为电话格式.但每次我使用格式化字符串设置EditText文本时,再次调用观察者,再次设置文本,并且它会陷入此无限循环中.
@Override
public void afterTextChanged(Editable editable) {
if (editable.length() > 1) {
try {
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
PhoneNumber numberProto = phoneUtil.parse(editable.toString(),"BR");
String formatted = phoneUtil.format(numberProto,PhoneNumberFormat.NATIONAL);
telephone.setText(formatted);
} catch (NumberParseException e) {
Log.d("Telefone","NumberParseException was thrown: " + e.toString());
}
}
}
最佳答案
原文链接:https://www.f2er.com/android/431062.html