【问题标题】:Hw to Format Currency Input editText? Xamarin.Android如何格式化货币输入editText? Xamarin.Android
【发布时间】:2021-08-02 05:22:08
【问题描述】:

我有一个 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();

【问题讨论】:

    标签: c# xamarin xamarin.android


    【解决方案1】:

    您可以使用下面的代码。我使用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="$0.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}", "$0.0", text.Substring(4));
            }
            return string.Format("{0}{1}", "$0.0", text);
        }
    }
    

    【讨论】:

    • 您能否提供正确的值格式?我需要更多详细信息。
    【解决方案2】:

    最简单的方法是在您的共享项目中创建一个名为 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>
    

    【讨论】:

    • 我正在使用 Xamarin 本机应用程序,即 Xamarin.ANdroid
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多