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

如何在.NET MAUI跨平台应用中保持屏幕键盘持续开启?

.NET MAUI聊天Entry保持键盘持续开启的解决方案

在聊天场景中,Entry按下回车后默认会失去焦点导致键盘收起,直接调用Focus()会出现键盘闪烁的问题。要实现跨平台的键盘持续开启,需要通过自定义Handler(MAUI推荐的平台扩展方式)修改各平台的原生行为,拦截回车事件的默认处理逻辑。


1. 注册平台自定义Handler

首先在MauiProgram.cs中为Entry注册不同平台的自定义Handler:

builder.ConfigureMauiHandlers(handlers =>
{
#if ANDROID
    handlers.AddHandler(typeof(Entry), typeof(ChatEntryHandlerAndroid));
#elif IOS
    handlers.AddHandler(typeof(Entry), typeof(ChatEntryHandlerIOS));
#endif
});

如果需要区分普通Entry和聊天专用Entry,可以创建一个自定义ChatEntry类,后续注册时替换为typeof(ChatEntry)即可。


2. Android平台实现

Android上需要拦截EditorAction事件,阻止回车触发的默认焦点丢失行为:

using Android.Views.InputMethods;
using Microsoft.Maui.Platform;

namespace YourAppNamespace.Platforms.Android;

public class ChatEntryHandlerAndroid : EntryHandler
{
    protected override void ConnectHandler(MauiTextView platformView)
    {
        base.ConnectHandler(platformView);
        platformView.EditorAction += OnEditorAction;
    }

    protected override void DisconnectHandler(MauiTextView platformView)
    {
        platformView.EditorAction -= OnEditorAction;
        base.DisconnectHandler(platformView);
    }

    private void OnEditorAction(object? sender, Android.Widget.TextView.EditorActionEventArgs e)
    {
        // 拦截回车/发送键的默认处理
        if (e.ActionId == ImeAction.Done || e.ActionId == ImeAction.Send)
        {
            e.Handled = true;
            // 触发Entry的Completed事件,执行你的消息发送逻辑
            (VirtualView as Entry)?.Completed?.Invoke(VirtualView, EventArgs.Empty);
            // 强制保持焦点,避免键盘收起
            platformView.RequestFocus();
        }
    }
}

3. iOS平台实现

iOS上需要实现ShouldReturn方法,阻止系统默认的键盘收起行为:

using UIKit;
using Microsoft.Maui.Platform;

namespace YourAppNamespace.Platforms.iOS;

public class ChatEntryHandlerIOS : EntryHandler
{
    protected override void ConnectHandler(MauiTextField platformView)
    {
        base.ConnectHandler(platformView);
        // 设置键盘返回键为"发送"(可选,优化UI体验)
        platformView.ReturnKeyType = UIReturnKeyType.Send;
        platformView.ShouldReturn += OnShouldReturn;
    }

    protected override void DisconnectHandler(MauiTextField platformView)
    {
        platformView.ShouldReturn -= OnShouldReturn;
        base.DisconnectHandler(platformView);
    }

    private bool OnShouldReturn(MauiTextField textField)
    {
        // 触发Completed事件执行消息发送
        (VirtualView as Entry)?.Completed?.Invoke(VirtualView, EventArgs.Empty);
        // 返回false阻止系统默认的键盘收起逻辑
        return false;
    }
}

4. 消息发送逻辑处理

现在只需要在Entry的Completed事件中处理消息发送,无需再调用Focus():

private void OnMessageEntryCompleted(object sender, EventArgs e)
{
    if (sender is Entry entry && !string.IsNullOrWhiteSpace(entry.Text))
    {
        // 将消息添加到聊天列表
        ChatMessages.Add(entry.Text);
        // 清空输入框
        entry.Text = string.Empty;
        // 无需手动调用Focus(),键盘会保持开启状态
    }
}

注意事项

  • Android端可以在XAML中设置ReturnType="Send",让键盘显示"发送"按钮,更贴合聊天场景。
  • 测试时需覆盖不同版本的Android/iOS系统,确保兼容性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 15:55:16