Xamarin 表单条目:使用逗号作为小数点分隔符

Mir*_*rja 2 xamarin xamarin.forms

我想在带有数字键盘的 Xamarin Forms 条目中使用逗号作为小数点分隔符。我将 Culture 设置为CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("de-DE");并且,显示在键盘上,但该条目仅接受.作为分隔符,并且仅当我使用电话键盘时才有效。这似乎是一个Andoid Bug。Xamarin Forms 有解决方案吗?

Mar*_*hel 6

有。

为了让它正常工作,我建议您执行以下操作:

在您的共享代码中创建一个名为“NumericInput”的新类,从 Entry 派生:

public class NumericInput : Entry
{
    public static BindableProperty AllowNegativeProperty = BindableProperty.Create("AllowNegative", typeof(bool), typeof(NumericInput), false, BindingMode.TwoWay);
    public static BindableProperty AllowFractionProperty = BindableProperty.Create("AllowFraction", typeof(bool), typeof(NumericInput), false, BindingMode.TwoWay);

    public NumericInput()
    {
        this.Keyboard = Keyboard.Numeric;
    }

    public bool AllowNegative
    {
        get { return (bool)GetValue(AllowNegativeProperty); }
        set { SetValue(AllowNegativeProperty, value); }
    }

    public bool AllowFraction
    {
        get { return (bool)GetValue(AllowFractionProperty); }
        set { SetValue(AllowFractionProperty, value); }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在您的 android 项目中为其创建一个自定义渲染器:

[assembly: ExportRenderer(typeof(NumericInput), typeof(NumericInputRenderer))]
namespace MyApp.Droid.Renderer
{
public class NumericInputRenderer : EntryRenderer
{
    public NumericInputRenderer(Context context) : base(context)
    {

    }

    private EditText _native = null;

    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if (e.NewElement == null)
            return;

        _native = Control as EditText;
        _native.InputType = Android.Text.InputTypes.ClassNumber;
        if ((e.NewElement as NumericInput).AllowNegative == true)
            _native.InputType |= InputTypes.NumberFlagSigned;
        if ((e.NewElement as NumericInput).AllowFraction == true)
        {
            _native.InputType |= InputTypes.NumberFlagDecimal;
            _native.KeyListener = DigitsKeyListener.GetInstance(string.Format("1234567890{0}", System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator));
        }
        if (e.NewElement.FontFamily != null)
        {
            var font = Typeface.CreateFromAsset(Android.App.Application.Context.Assets, e.NewElement.FontFamily);
            _native.Typeface = font;
        }
    }

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);
        if (_native == null)
            return;

        if (e.PropertyName == NumericInput.AllowNegativeProperty.PropertyName)
        {
            if ((sender as NumericInput).AllowNegative == true)
            {
                // Add Signed flag
                _native.InputType |= InputTypes.NumberFlagSigned;
            }
            else
            {
                // Remove Signed flag
                _native.InputType &= ~InputTypes.NumberFlagSigned;
            }
        }
        if (e.PropertyName == NumericInput.AllowFractionProperty.PropertyName)
        {
            if ((sender as NumericInput).AllowFraction == true)
            {
                // Add Decimal flag
                _native.InputType |= InputTypes.NumberFlagDecimal;
                _native.KeyListener = DigitsKeyListener.GetInstance(string.Format("1234567890{0}", System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator));
            }
            else
            {
                // Remove Decimal flag
                _native.InputType &= ~InputTypes.NumberFlagDecimal;
                _native.KeyListener = DigitsKeyListener.GetInstance(string.Format("1234567890"));
            }
        }
    }
}
}
Run Code Online (Sandbox Code Playgroud)

这将创建一个条目元素,它会根据设备的当前文化设置自动使用正确的小数点分隔符。

由于该类允许进行一些详细设置,因此我还应该添加 iOS 渲染器:

[assembly: ExportRenderer(typeof(NumericInput), typeof(NumericInputRenderer))]
namespace MyApp.iOS.Renderer
{
public class NumericInputRenderer : EntryRenderer
{
    private UITextField _native = null;

    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if (e.NewElement == null)
            return;

        _native = Control as UITextField;

        _native.KeyboardType = UIKeyboardType.NumberPad;

        if ((e.NewElement as NumericInput).AllowNegative == true && (e.NewElement as NumericInput).AllowFraction == true)
        {
            _native.KeyboardType = UIKeyboardType.NumbersAndPunctuation;
        } 
        else if ((e.NewElement as NumericInput).AllowNegative == true)
        {
            _native.KeyboardType = UIKeyboardType.NumbersAndPunctuation;
        }
        else if ((e.NewElement as NumericInput).AllowFraction == true)
        {
            _native.KeyboardType = UIKeyboardType.DecimalPad;
        }
        else
        {
            _native.KeyboardType = UIKeyboardType.NumberPad;
        }
        if (e.NewElement.FontFamily != null)
        {
            e.NewElement.FontFamily = e.NewElement.FontFamily.Replace(".ttf", "");
        }
    }

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);
        if (_native == null)
            return;
    }
}
}
Run Code Online (Sandbox Code Playgroud)

但是请记住,一些供应商已经实现了自定义软键盘(我在看你,三星!),甚至不显示逗号作为十进制分隔符。在这种情况下,唯一的解决方案是安装另一个键盘,例如 SwiftKey 或 Gboard。但是,如果显示逗号,您应该可以将它与上面的代码一起使用。