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

在.NET MAUI中为Entry设置数字键盘(iPadOS环境下遇阻)

.NET MAUI iPadOS 数字键盘不生效的解决办法

在iPadOS上,设置Keyboard="Numeric"会弹出带字母栏的混合数字键盘,这是苹果的默认设计。如果需要纯数字键盘,需要直接指定iOS原生的UIKeyboardType.NumberPad类型,以下是两种可行方案:

方案一:全局自定义Entry Handler

在项目的Platforms/iOS目录下创建自定义Handler类:

using Microsoft.Maui.Handlers;
using UIKit;

namespace YourAppName.Platforms.iOS;

public class NumericEntryHandler : EntryHandler
{
    protected override void ConnectHandler(UITextField platformView)
    {
        base.ConnectHandler(platformView);
        // 仅针对iPad设备设置纯数字键盘
        if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
        {
            platformView.KeyboardType = UIKeyboardType.NumberPad;
        }
    }
}

然后在MauiProgram.cs中注册该Handler:

builder.ConfigureMauiHandlers(handlers =>
{
#if IOS
    handlers.AddHandler<Entry, NumericEntryHandler>();
#endif
});

方案二:单个Entry针对性设置

如果仅需要给特定Entry设置纯数字键盘,可以在页面的代码后台添加逻辑:

protected override void OnHandlerChanged()
{
    base.OnHandlerChanged();
#if IOS
    // 替换为你的Entry控件名称
    if (YourEntry.Handler?.PlatformView is UITextField textField && UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
    {
        textField.KeyboardType = UIKeyboardType.NumberPad;
    }
#endif
}

执行上述操作后,iPad上的Entry控件会弹出纯数字键盘,和iOS手机端的数字键盘一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 13:18:06