如何使用Xamarin for Android格式化电话号码?

时间:2015-05-24 23:58:13

标签: c# android xamarin

如何使用Xamarin for Android格式化电话号码?我试过这段代码:

EditText inputField = (EditText) FindViewById(Resource.Id.editMensagemTelefone);
inputField.AddTextChangedListener(new PhoneNumberFormattingTextWatcher());

2 个答案:

答案 0 :(得分:4)

确保在使用PhoneNumberFormattingTextWatcher()

时使用Android.Telephony命名空间

请参阅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;
                }
            }
        });