如何格式化货币输入editText? Xamarin.Android

Hw to Format Currency Input editText? Xamarin.Android

我有一个 editText,起始值为 $0.00。当您按 1 时,它会变为 $0.01。按 4,它变为 0.14 美元。按 8,按 0,然后按 1.40,1.48 美元。按退格键、$0.14 等。 谁能帮我 我在下面使用

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

最简单的方法是在您的共享项目中创建一个名为 CurrencyBehavior.cs 的文件。

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

然后在您的 xaml 文件中:

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

您可以使用下面的代码。我使用 android:hint 将占位符文本 ($0.00) 设置为 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="[=10=].00"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

后面的代码:

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}", "[=11=].0", text.Substring(4));
        }
        return string.Format("{0}{1}", "[=11=].0", text);
    }
}