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

.NET MAUI Entry控件点击外部无法隐藏键盘 求解决方案

MAUI Entry点击外部无法失焦收键盘及按钮被遮挡解决方案

一、解决Entry点击外部无法失焦收键盘问题(Android端)

针对MAUI的已知Bug,可通过自定义Android平台渲染器捕获外部触摸事件,强制Entry失焦并收起键盘:

  1. 在Android项目中创建自定义Entry渲染器类:
using Android.Content;
using Android.Views;
using Microsoft.Maui.Controls.Platform;
using Microsoft.Maui.Platform;

namespace YourAppNamespace.Platforms.Android;

public class CustomEntryRenderer : EntryRenderer
{
    public CustomEntryRenderer(Context context) : base(context)
    {
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if (Control != null && Parent is View parentView)
        {
            parentView.SetOnTouchListener(new EntryTouchListener(Control));
        }
    }

    private class EntryTouchListener : Java.Lang.Object, View.IOnTouchListener
    {
        private readonly EditText _entryControl;

        public EntryTouchListener(EditText entryControl)
        {
            _entryControl = entryControl;
        }

        public bool OnTouch(View v, MotionEvent e)
        {
            if (e.Action == MotionEventActions.Down)
            {
                var rect = new Rect();
                _entryControl.GetGlobalVisibleRect(rect);
                // 判断点击坐标是否在Entry区域外
                if (!rect.Contains((int)e.RawX, (int)e.RawY))
                {
                    _entryControl.ClearFocus();
                    // 收起软键盘
                    var imm = (InputMethodManager)_entryControl.Context.GetSystemService(Context.InputMethodService);
                    imm.HideSoftInputFromWindow(_entryControl.WindowToken, 0);
                }
            }
            return false;
        }
    }
}
  1. 在MauiProgram.cs中注册渲染器:
builder.ConfigureMauiHandlers(handlers =>
{
#if ANDROID
    handlers.AddHandler(typeof(Entry), typeof(Platforms.Android.CustomEntryRenderer));
#endif
});

二、解决底部按钮被软键盘遮挡问题

方法1:启用窗口自动调整

在页面的OnAppearing方法中设置Android窗口的软键盘模式为AdjustResize,让页面自动调整布局:

protected override void OnAppearing()
{
    base.OnAppearing();

#if ANDROID
    Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.Window.SetSoftInputMode(Android.Views.SoftInput.AdjustResize);
#endif
}

方法2:用ScrollView包裹页面内容

将整个页面内容放入ScrollView中,软键盘弹出时页面会自动滚动,确保底部按钮可见:

<ScrollView>
    <VerticalStackLayout>
        <Entry x:Name="PhoneEntry" Placeholder="输入手机号" />
        <!-- 其他页面元素 -->
        <Button Text="提交" VerticalOptions="End" Margin="0,20,0,0" />
    </VerticalStackLayout>
</ScrollView>

三、简易替代方案(无需渲染器)

若不想编写平台特定代码,可通过透明遮罩捕获外部点击:

  1. 在XAML中添加透明遮罩(仅在Entry获焦时显示):
<Grid>
    <VerticalStackLayout>
        <Entry x:Name="PhoneEntry" Placeholder="输入手机号" 
               Focused="PhoneEntry_Focused" 
               Unfocused="PhoneEntry_Unfocused" />
        <Button Text="提交" VerticalOptions="End" />
    </VerticalStackLayout>

    <BoxView x:Name="ClickMask" BackgroundColor="Transparent" IsVisible="False">
        <BoxView.GestureRecognizers>
            <TapGestureRecognizer Tapped="ClickMask_Tapped" />
        </BoxView.GestureRecognizers>
    </BoxView>
</Grid>
  1. 后台处理焦点切换与键盘收起:
private void PhoneEntry_Focused(object sender, FocusEventArgs e)
{
    ClickMask.IsVisible = true;
}

private void PhoneEntry_Unfocused(object sender, FocusEventArgs e)
{
    ClickMask.IsVisible = false;
}

private void ClickMask_Tapped(object sender, EventArgs e)
{
    PhoneEntry.Unfocus();
#if ANDROID
    var imm = Android.App.Application.Context.GetSystemService(Android.Content.Context.InputMethodService) as Android.Views.InputMethods.InputMethodManager;
    imm?.HideSoftInputFromWindow(Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.CurrentFocus?.WindowToken, 0);
#endif
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 16:20:23