how can i format phone number xamarin android ?

tadrianotadriano USMember
edited May 2015 in Xamarin.Android

how can i format phone number xamarin android ? I tried this code below:

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

Posts

  • DimitarIvanovDimitarIvanov USMember ✭✭

    @tadriano Check out this -> http://developer.android.com/reference/android/telephony/PhoneNumberUtils.html
    With this you can format it : PhoneNumberUtils.formatNumber(Editable text, int defaultFormattingType);

  • tadrianotadriano USMember

    I tried this, but not work for me. Can you send me your example? I'm trying to change for BR.

  • AsaasAsaas BRMember

    Hi Thiago, how are you? i maked something like this.

    private EditText FirstStepActivationNumberText;
    private bool EditTextEnable = true;

    protected override void OnCreate (Bundle bundle)
    {
    .............
    FirstStepActivationNumberText = this.FindViewById (Resource.Id.FirstStepActivationNumberText);
    FirstStepActivationNumberText.SetSingleLine ();
    FirstStepActivationNumberText.TextChanged += HandleTextChanged;
    FirstStepActivationNumberText.AfterTextChanged += HandleAfterTextChanged;
    ........
    }

    void HandleTextChanged (object sender, Android.Text.TextChangedEventArgs e)
        {
            if (FirstStepActivationNumberText.Text.Length == 1) { 
                string Value = FirstStepActivationNumberText.Text;
                if(!Value.Contains("(")) {
                    FirstStepActivationNumberText.Text = "("+ Value; 
                }
            }
    
            if (FirstStepActivationNumberText.Text.Length == 3) { 
                string Value = FirstStepActivationNumberText.Text;
                if (!Value.Contains (")")) {
                    FirstStepActivationNumberText.Text = Value + ")";   
                }
            }
    
            FirstStepActivationNumberText.SetSelection (FirstStepActivationNumberText.Text.Length);
        }
    
        public void HandleAfterTextChanged (object sender, Android.Text.AfterTextChangedEventArgs e)
        {           
            try {   
                if (EditTextEnable) {           
                    EditTextEnable = false;                                             
                }
            }
            catch {};
        }
    
Sign In or Register to comment.