Wie wird die Währungseingabe editText formatiert? Xamarin.Android

Wie wird die Währungseingabe editText formatiert? Xamarin.Android

Lösung für Hw zum Formatieren der Währungseingabe editText? Xamarin.Android
ist unten angegeben:

Ich habe einen editText, Startwert ist 0,00 $. Wenn Sie 1 drücken, ändert es sich in 0,01 $. Drücken Sie 4, es geht zu $0,14. Drücken Sie 8, drücken Sie 0, dann 1,40, $1,48. Drücken Sie die Rücktaste, $0,14 usw.
Kann mir jemand helfen
Ich verwende unten

            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();

Am einfachsten ist es, eine Datei namens CurrencyBehavior.cs in Ihrem freigegebenen Projekt zu erstellen.

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;
            }
        }
 
 
    }

Und dann in Ihrer XAML-Datei:

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

Sie könnten den folgenden Code verwenden. Ich verwende den android:hint um den Platzhaltertext ($0.00) auf EditText.

zu setzen
<?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>

Code dahinter:

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);
    }
}