gpt4 book ai didi

Xamarin 表单条目 : Use comma as decimal separator

转载 作者:行者123 更新时间:2023-12-02 17:05:50 32 4
gpt4 key购买 nike

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

最佳答案

有。

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

在您的共享代码中创建一个名为“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); }
}
}

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

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

由于该类允许进行一些详细设置,因此我还应该添加 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。但是,如果显示逗号,您应该可以在上面的代码中使用它。

关于Xamarin 表单条目 : Use comma as decimal separator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51764129/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com