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

MAUI安卓端如何通过Entry的Text Completed事件收起软键盘

MAUI安卓平台Entry Completed事件触发时收起软键盘实现方案

该需求可直接实现,无需依赖第三方组件,适配.NET 7、.NET 8、.NET 9所有正式版MAUI框架。旧版Xamarin.Forms的同类方案只需调整平台控件的获取逻辑,适配MAUI的Handler体系即可正常使用。

方法一:单页面快速实现

适合仅个别页面需要该交互的场景,直接绑定事件编写逻辑即可:

  • 首先在页面的Entry控件上绑定Completed事件,XAML示例写法:
    <Entry x:Name="TestInputEntry" Placeholder="输入完成按回车测试" Completed="TestInputEntry_Completed"/>
    
  • 在页面对应的后台代码中编写事件处理逻辑,仅在安卓平台执行软键盘收起操作,避免跨平台异常:
    using Microsoft.Maui.Platform;
    using Android.Views.InputMethods;
    
    private void TestInputEntry_Completed(object sender, EventArgs e)
    {
        if (DeviceInfo.Current.Platform != DevicePlatform.Android)
            return;
    
        // 获取Entry对应的安卓原生控件实例
        var platformEntry = TestInputEntry.ToPlatform(Handler.MauiContext);
        // 获取系统输入法管理器服务
        var inputManager = platformEntry.Context.GetSystemService(Android.Content.Context.InputMethodService) as InputMethodManager;
        // 调用系统API收起软键盘
        inputManager?.HideSoftInputFromWindow(platformEntry.WindowToken, 0);
    }
    

MAUI和旧版Xamarin.Forms的核心差异点:旧版框架通过Renderer属性获取原生控件,MAUI统一改为通过ToPlatform()方法结合MauiContext获取平台控件,旧的Renderer相关API已被标记为过时,不建议在新项目中使用。

方法二:全局附加属性实现(全项目复用)

适合项目中大量Entry都需要完成后自动收起软键盘的场景,一次编写后所有控件可直接复用,无需每个页面重复写事件逻辑:

  • 新建静态类编写附加属性,代码示例:
    using Microsoft.Maui.Platform;
    using Android.Views.InputMethods;
    
    namespace YourMauiProjectRootNamespace;
    
    public static class EntryHideKeyboardBehavior
    {
        public static readonly BindableProperty EnableHideOnCompletedProperty =
            BindableProperty.CreateAttached(
                "EnableHideOnCompleted",
                typeof(bool),
                typeof(EntryHideKeyboardBehavior),
                false,
                propertyChanged: OnEnableStateChanged);
    
        public static bool GetEnableHideOnCompleted(BindableObject bindable) => (bool)bindable.GetValue(EnableHideOnCompletedProperty);
        public static void SetEnableHideOnCompleted(BindableObject bindable, bool value) => bindable.SetValue(EnableHideOnCompletedProperty, value);
    
        private static void OnEnableStateChanged(BindableObject bindable, object oldValue, object newValue)
        {
            if (bindable is not Entry targetEntry)
                return;
    
            if ((bool)newValue)
                targetEntry.Completed += OnEntryCompleted;
            else
                targetEntry.Completed -= OnEntryCompleted;
        }
    
        private static void OnEntryCompleted(object sender, EventArgs e)
        {
            if (DeviceInfo.Current.Platform != DevicePlatform.Android)
                return;
            if (sender is not Entry entry || entry.Handler?.MauiContext == null)
                return;
    
            var platformEntry = entry.ToPlatform(entry.Handler.MauiContext);
            var inputManager = platformEntry.Context.GetSystemService(Android.Content.Context.InputMethodService) as InputMethodManager;
            inputManager?.HideSoftInputFromWindow(platformEntry.WindowToken, 0);
        }
    }
    
  • 在XAML中引入该静态类的命名空间后,直接给需要功能的Entry附加属性即可,无需编写后台代码:
    <Entry Placeholder="输入完成自动收键盘" local:EntryHideKeyboardBehavior.EnableHideOnCompleted="True"/>
    

常见踩坑说明

  • 不要通过调用Entry.Unfocus()的方式间接收起软键盘,该逻辑在MIUI、ColorOS等国产定制安卓系统上会触发焦点异常,表现为软键盘收起后立刻重新弹出
  • 调用HideSoftInputFromWindow方法时必须传入当前输入控件对应的WindowToken,传入全局应用上下文的Token会导致软键盘收起逻辑完全失效
  • 上述实现不需要额外声明安卓权限,调用的是安卓系统公开的输入法控制API,不存在应用商店上架合规问题
  • 代码中已添加平台判断逻辑,不会干扰iOS、Windows、MacCatalyst平台的默认交互行为

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 08:06:19