Create a mask for cpf using xamarin

4

Hello, I would like to create a mask for cpf using edittext, but I was not successful; I searched the net and found several examples in java, but I could not adapt to C #, could anyone help me?

Thank you in advance.

    
asked by anonymous 23.12.2015 / 17:42

3 answers

4

I have improved the code to make it easier to reuse the code, as follows:

public class Mask : Java.Lang.Object, ITextWatcher
{
    private readonly EditText _editText;
    private readonly string _mask;
    bool isUpdating;
    string old = "";

    public Mask(EditText editText, string mask)
    {
        _editText = editText;
        _mask = mask;
    }

    public static string Unmask(string s)
    {
        return s.Replace(".", "").Replace("-", "")
            .Replace("/", "").Replace("(", "")
            .Replace(")", "");
    }

    public void AfterTextChanged(IEditable s)
    {
    }

    public void BeforeTextChanged(ICharSequence s, int start, int count, int after)
    {
    }

    public void OnTextChanged(ICharSequence s, int start, int before, int count)
    {
        string str = Unmask(s.ToString());
        string mascara = "";

        if (isUpdating)
        {
            old = str;
            isUpdating = false;
            return;
        }

        int i = 0;

        foreach (var m in _mask.ToCharArray())
        {
            if (m != '#' && str.Length > old.Length)
            {
                mascara += m;
                continue;
            }
            try
            {
                mascara += str[i];
            }
            catch (System.Exception ex)
            {
                break;
            }
            i++;
        }

        isUpdating = true;

        _editText.Text = mascara;

        _editText.SetSelection(mascara.Length);
    }
}

Calling the Activity:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
        var txtCpf = _view.FindViewById<EditText>(Resource.Id.txtCpf);
        txtCpf.AddTextChangedListener(new Mask(txtCpf, "###.###.###-##"));
}
    
26.07.2016 / 16:31
4

Well, if anyone wants something simpler in PCL just add this function in TextChanged's entry, textCell

        void cpfMask(object sender, EventArgs e)
        {
            var ev = e as TextChangedEventArgs;

            if (ev.NewTextValue != ev.OldTextValue)
            {
                var entry = (Entry)sender;
                string text = Regex.Replace(ev.NewTextValue, @"[^0-9]", "");

                text = text.PadRight(11);

                // removendo todos os digitos excedentes 
                if (text.Length > 11)
                {
                    text = text.Remove(11);
                }

                text = text.Insert(3, ".").Insert(7, ".").Insert(11, "-").TrimEnd(new char[] { ' ', '.', '-' });
                if (entry.Text != text)
                    entry.Text = text;

            }
        }
    
12.09.2016 / 14:49
1

Well guys, after a few hours studying, I managed to do it and I decided to share it with you, in case anyone has an interest. In fact I simply adapted it to c# , the original code in java is in this link .

 protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);


        SetContentView(Resource.Layout.Main);

        et = FindViewById<EditText>(Resource.Id.edittext1);
        var ed = FindViewById<EditText>(Resource.Id.edittext2);

        insert("###.###.###-##", et);


       et.AddTextChangedListener(this);



    }
    public static string unmask(string s)
    {
        return s.Replace(".", "").Replace("-", "")
                .Replace("/", "").Replace("(", "")
                .Replace(")", "");
    }

    public void insert(string mask, EditText ediTxt)
    {
        _mask = mask;
        et = ediTxt;
    }

    public void AfterTextChanged(IEditable s)
    {
    }



    public void BeforeTextChanged(ICharSequence s, int start, int count, int after)
    {

    }

    public void OnTextChanged(ICharSequence s, int start, int before, int count)
    {
        string str = unmask(s.ToString());
        string mascara = "";

        if (isUpdating)
        {
            old = str;
            isUpdating = false;
            return;

        }

        int i = 0;

        foreach (var m in _mask.ToCharArray())
        {
            if (m != '#' && str.Length > old.Length)
            {
                mascara += m;
                continue;
            }
            try
            {
                mascara += str[i];
            }
            catch (Exception e)
            {

                break;
            }
            i++;
        }
        isUpdating = true;
        et.Text = mascara;
        et.SetSelection(mascara.Length);


    }
}

}

    
24.12.2015 / 18:43