You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin Forms 5.0.0.2587丹麦语环境数字键盘逗号输入无响应求助

解决Xamarin Forms 5 EntryCell数字键盘无法输入丹麦语小数逗号的问题

问题根源

Xamarin Forms默认的Keyboard.Numeric在Android端未正确适配区域设置的小数分隔符,即便已设置线程文化,系统键盘的输入规则仍受Android原生控件配置限制,导致丹麦语环境下的逗号分隔符无法正常输入。

解决方案

1. 自定义Android EntryCell渲染器

针对EntryCell编写自定义渲染器,强制配置输入类型并指定允许的小数分隔符:

[assembly: ExportRenderer(typeof(EntryCell), typeof(CustomNumericEntryCellRenderer))]
namespace YourAppNamespace.Droid.Renderers
{
    public class CustomNumericEntryCellRenderer : EntryCellRenderer
    {
        protected override Android.Views.View GetCellCore(Cell item, Android.Views.View convertView, ViewGroup parent, Context context)
        {
            var cell = base.GetCellCore(item, convertView, parent, context);
            if (cell is EntryCellView entryCellView)
            {
                var editText = entryCellView.EditText;
                // 配置输入类型为数字+小数
                editText.InputType = Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberDecimal;
                
                // 获取丹麦语的小数分隔符(逗号)
                var decimalSeparator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
                // 设置允许输入的字符:数字+当前区域的小数分隔符
                editText.KeyListener = DigitsKeyListener.GetInstance($"0123456789{decimalSeparator}");
            }
            return cell;
        }
    }
}

2. 补充Android系统层面区域配置

仅设置.NET线程文化可能不够,在MainActivity的OnCreate方法中同步配置Android原生Locale:

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    var daCulture = new CultureInfo("da-DK");
    // 设置.NET线程文化
    Thread.CurrentThread.CurrentCulture = daCulture;
    Thread.CurrentThread.CurrentUICulture = daCulture;
    CultureInfo.DefaultThreadCurrentCulture = daCulture;
    CultureInfo.DefaultThreadCurrentUICulture = daCulture;

    // 设置Android原生Locale
    var androidLocale = new Java.Util.Locale("da", "DK");
    Java.Util.Locale.Default = androidLocale;
    Resources.Configuration.Locale = androidLocale;
    Resources.UpdateConfiguration(Resources.Configuration, Resources.DisplayMetrics);

    Xamarin.Essentials.Platform.Init(this, savedInstanceState);
    global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
    LoadApplication(new App());
}

3. 替代方案:改用Entry控件

若EntryCell的渲染器修改存在阻碍,可替换为Entry控件,搭配自定义渲染器灵活性更高:

XAML代码:

<Entry Keyboard="Numeric" Placeholder="输入数字" />

对应的Entry自定义渲染器(Android端):

[assembly: ExportRenderer(typeof(Entry), typeof(CustomNumericEntryRenderer))]
namespace YourAppNamespace.Droid.Renderers
{
    public class CustomNumericEntryRenderer : EntryRenderer
    {
        public CustomNumericEntryRenderer(Context context) : base(context) { }

        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
                Control.InputType = Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberDecimal;
                var decimalSeparator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
                Control.KeyListener = DigitsKeyListener.GetInstance($"0123456789{decimalSeparator}");
            }
        }
    }
}

测试注意事项

  • 确保测试设备的系统语言和区域格式均设置为丹麦语(丹麦),两者分开设置可能导致适配失效。
  • 测试前清理应用缓存并重启,避免旧配置残留。

内容的提问来源于stack exchange,提问作者Ja Da

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 04:46:30