如何使用Xamarin for Android格式化电话号码?我试过这段代码:
EditText inputField = (EditText) FindViewById(Resource.Id.editMensagemTelefone);
inputField.AddTextChangedListener(new PhoneNumberFormattingTextWatcher());
答案 0 :(得分:4)
确保在使用PhoneNumberFormattingTextWatcher()
请参阅https://developer.xamarin.com/api/type/Android.Telephony.PhoneNumberUtils/
答案 1 :(得分:1)
Try this one:
_edtphonenumber=(EditText)findViewById(R.id.editText_phonenumber);
_edtphonenumber.setTypeface(trajan_pro_regular);
_edtphonenumber.addTextChangedListener(new TextWatcher() {
private boolean mFormatting; // this is a flag which prevents the stack overflow.
private int mAfter;
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// nothing to do here..
}
//called before the text is changed...
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//nothing to do here...
mAfter = after; // flag to detect backspace..
}
@Override
public void afterTextChanged(Editable s) {
// Make sure to ignore calls to afterTextChanged caused by the work done below
if (!mFormatting) {
mFormatting = true;
// using US formatting...
if(mAfter!=0) // in case back space ain't clicked...
PhoneNumberUtils.formatNumber(s,PhoneNumberUtils.getFormatTypeForLocale(Locale.US));
mFormatting = false;
}
}
});