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

时间:2018-08-09 10:01:57

标签: xamarin xamarin.forms

我想在数字键盘的Xamarin Forms条目中使用逗号作为小数点分隔符。我将区域性设置为,,键盘上显示了.,但是该条目仅接受<div class="container"> <div class="row"> <div class="col-sm-12" style=""> dog </div> </div> <div class="row"> <div class="col-sm-12" style=""> </div> </div> <div class="row"> <div class="col-sm-12" style=""> cat </div> </div> </div> 作为分隔符,并且仅在使用电话键盘时才有效。这似乎是一个Andoid Bug。 Xamarin Forms是否有解决方案?

1 个答案:

答案 0 :(得分:1)

有。

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

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

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

然后在您的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"));
            }
        }
    }
}
}

这将创建一个entry元素,该元素将根据设备的当前区域性设置自动使用正确的小数点分隔符。

由于该类允许进行一些详细的设置,因此我还应该添加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;
    }
}
}

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

相关问题