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

.NET MAUI能否自定义键盘按键?需为数字键盘添加/与X键(适配安卓iOS)

.NET MAUI自定义数字键盘添加"/"和"X"按键方案

这个方案完全可行,系统标准数字键盘无法满足需求时,通过针对Android和iOS平台分别实现自定义渲染器,就能扩展出包含"/"和"X"的专用键盘。以下是具体实现思路和代码示例:

Android端实现

Android可以通过自定义键盘布局文件+自定义渲染器的方式实现:

  1. 在Android平台项目的Resources/Layout目录下创建自定义键盘布局keyboard_bowling.xml,定义数字键、"/"、"X"键的排列:
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyWidth="33%p"
    android:horizontalGap="0px"
    android:verticalGap="0px">
    <Row>
        <Key android:codes="49" android:keyLabel="1" />
        <Key android:codes="50" android:keyLabel="2" />
        <Key android:codes="51" android:keyLabel="3" />
    </Row>
    <Row>
        <Key android:codes="52" android:keyLabel="4" />
        <Key android:codes="53" android:keyLabel="5" />
        <Key android:codes="54" android:keyLabel="6" />
    </Row>
    <Row>
        <Key android:codes="55" android:keyLabel="7" />
        <Key android:codes="56" android:keyLabel="8" />
        <Key android:codes="57" android:keyLabel="9" />
    </Row>
    <Row>
        <Key android:codes="47" android:keyLabel="/" />
        <Key android:codes="48" android:keyLabel="0" />
        <Key android:codes="88" android:keyLabel="X" />
    </Row>
</Keyboard>
  1. 实现自定义Entry渲染器,关联自定义键盘并处理按键事件:
[assembly: ExportRenderer(typeof(Entry), typeof(BowlingEntryRenderer), new[] { typeof(CustomKeyboardVisual) })]
namespace YourAppName.Droid.Renderers;

public class BowlingEntryRenderer : EntryRenderer
{
    private Android.InputMethodServices.Keyboard _customKeyboard;
    private EditText _editText;

    public BowlingEntryRenderer(Context context) : base(context)
    {
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);
        if (Control == null) return;
        
        _editText = Control;
        _customKeyboard = new Android.InputMethodServices.Keyboard(Context, Resource.Layout.keyboard_bowling);
        _editText.ShowSoftInputOnFocus = false;
        _editText.Touch += EditText_Touch;
    }

    private void EditText_Touch(object sender, TouchEventArgs e)
    {
        if (!_editText.HasFocus) return;
        e.Handled = true;
        
        var keyboardView = new KeyboardView(Context, null)
        {
            Keyboard = _customKeyboard,
            Visible = true
        };
        keyboardView.Key += KeyboardView_Key;
        
        var lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
        ((ViewGroup)_editText.RootView).AddView(keyboardView, lp);
    }

    private void KeyboardView_Key(object sender, KeyboardView.KeyEventArgs e)
    {
        if (e.PrimaryCode == -5) // 处理退格键
        {
            if (_editText.Text.Length > 0)
                _editText.Text = _editText.Text.Substring(0, _editText.Text.Length - 1);
        }
        else
        {
            _editText.Text += char.ToUpper((char)e.PrimaryCode);
        }
    }
}

iOS端实现

iOS通过自定义UIInputView替换系统键盘,同样用渲染器实现:

  1. 实现自定义Entry渲染器,创建包含目标按键的自定义键盘视图:
[assembly: ExportRenderer(typeof(Entry), typeof(BowlingEntryRenderer), new[] { typeof(CustomKeyboardVisual) })]
namespace YourAppName.iOS.Renderers;

public class BowlingEntryRenderer : EntryRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);
        if (Control == null) return;
        
        Control.InputView = CreateCustomKeyboard();
    }

    private UIView CreateCustomKeyboard()
    {
        var keyboardView = new UIView(new CGRect(0, 0, UIScreen.MainScreen.Bounds.Width, 200))
        {
            BackgroundColor = UIColor.LightGray
        };

        // 定义所有按键文本
        var buttonTitles = new[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "/", "0", "X" };
        int row = 0;
        int col = 0;
        
        foreach (var title in buttonTitles)
        {
            var button = UIButton.FromType(UIButtonType.System);
            button.SetTitle(title, UIControlState.Normal);
            button.Frame = new CGRect(col * 80, row * 50, 80, 50);
            button.TouchUpInside += (sender, _) => Control.Text += title;
            
            keyboardView.AddSubview(button);

            col++;
            if (col == 3)
            {
                col = 0;
                row++;
            }
        }

        // 添加退格按钮
        var backButton = UIButton.FromType(UIButtonType.System);
        backButton.SetTitle("←", UIControlState.Normal);
        backButton.Frame = new CGRect(240, 150, 80, 50);
        backButton.TouchUpInside += (sender, _) =>
        {
            if (Control.Text.Length > 0)
                Control.Text = Control.Text.Substring(0, Control.Text.Length - 1);
        };
        keyboardView.AddSubview(backButton);

        return keyboardView;
    }
}

共享项目配置

在共享项目中创建一个标记接口,用来标识需要使用自定义键盘的Entry:

public interface CustomKeyboardVisual
{
}

之后在XAML中给需要的Entry添加标记即可:

<Entry local:CustomKeyboardVisual="True" />

这种方式可以同时适配Android和iOS平台,完全满足保龄球计分应用的键盘需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 10:15:34