Come formattare l'input di valuta editText? Xamarin.Android

Come formattare l'input di valuta editText? Xamarin.Android

Soluzione per Hw per formattare l'input di valuta editText? Xamarin.Android
è riportato di seguito:

Ho un editText, il valore iniziale è $ 0,00. Quando si preme 1, cambia in $ 0,01. Premi 4, va a $ 0,14. Premi 8, premi 0 poi 1,40 , $ 1,48. Premi backspace, $ 0,14, ecc.
Qualcuno può aiutarmi
Sto usando di seguito

            System.String ss = editText.Text.ToString();
            System.String cleanString = inputKey.Replace("[$,.]", "");
            double parsed = Convert.ToDouble(cleanString);
            double currentd = Convert.ToDouble(current);
            System.String formatted = System.String.Format((parsed / 100).ToString());
            double formattedd = Convert.ToDouble(formatted);
            currentd = currentd * 10;
                double pp = currentd + formattedd;
                current = pp.ToString();
                editText.Text = pp.ToString();

Il modo più semplice è creare un file chiamato CurrencyBehavior.cs nel tuo progetto condiviso.

public class CurrencyBehavior : Behavior<Entry>
    {
        private bool _hasFormattedOnce = false;
        protected override void OnAttachedTo(Entry entry)
        {
            entry.TextChanged += OnEntryTextChanged;
            entry.Focused += EntryOnFocused;
            entry.Unfocused += EntryOnUnfocused;
            base.OnAttachedTo(entry);
        }
 
        private void EntryOnUnfocused(object sender, FocusEventArgs e)
        {
            var entry = sender as Entry;
            if (entry?.Text.HasValues()==false)
            {
                entry.Text = "0.00";
            } 
        }
 
        private void EntryOnFocused(object sender, FocusEventArgs e)
        {
            var entry =  sender as Entry;
            if (entry?.Text == "0.00")
            {
                entry.Text = "";
            }
        }
 
        protected override void OnDetachingFrom(Entry entry)
        {
            entry.TextChanged -= OnEntryTextChanged;
            entry.Focused -= EntryOnFocused;
            entry.Unfocused -= EntryOnUnfocused;
            base.OnDetachingFrom(entry);
        }
 
        private   void OnEntryTextChanged(object sender, TextChangedEventArgs args)
        {
            if (!_hasFormattedOnce && args.NewTextValue == "0")
            {
                ((Entry) sender).Text = "0.00";
                _hasFormattedOnce = true;
            }
        }
 
 
    }

E poi nel tuo file xaml :

                 <Entry 
                       Text="{Binding MyMoneyPropertyInMyViewModel}"
                       Keyboard="Numeric">
                    <Entry.Behaviors>
                        <behaviors:CurrencyBehavior />
                    </Entry.Behaviors>
                </Entry>

Potresti usare il codice qui sotto. Uso il android:hint per impostare il testo segnaposto ($ 0,00) su EditText.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:id="@+id/myEditField"
        android:hint="$0.00"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

Codice dietro:

public class Activity2 : Activity
{
    private EditText _editText;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your application here

        SetContentView(Resource.Layout.layout2);

        _editText = FindViewById<EditText>(Resource.Id.myEditField);
        _editText.AfterTextChanged += EditText_AfterTextChanged;
    }

    private void EditText_AfterTextChanged(object sender, AfterTextChangedEventArgs e)
    {
        var text = e.Editable.ToString();
        _editText.AfterTextChanged -= EditText_AfterTextChanged;
        var formatedText = ValueConverter(text);
        _editText.Text = formatedText;
        _editText.SetSelection(formatedText.Length);
        _editText.AfterTextChanged += EditText_AfterTextChanged;
    }
    private static string ValueConverter(string text)
    {
        if (text.Length>5)
        {
            return string.Format("{0}{1}", "$0.0", text.Substring(4));
        }
        return string.Format("{0}{1}", "$0.0", text);
    }
}