如何在 .NET MAUI 中禁用控件焦点 (安卓)

本教程介绍了在 .NET MAUI 安卓应用中如何通过修改 Handler 来禁用控件(如 Entry 和 Button)的焦点,以避免弹出软键盘或获取不必要的焦点。

  1. 安卓
    在CreateMauiApp()方法添加如下代码。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    EntryHandler.Mapper.AppendToMapping("NoKeyboardEntry", (handler, entry) =>
                {
    #if ANDROID
                    // 隐藏软键盘
                    handler.PlatformView.ShowSoftInputOnFocus = false;
                    handler.PlatformView.ImeOptions = Android.Views.InputMethods.ImeAction.ImeNull;
                    handler.PlatformView.InputType = Android.Text.InputTypes.Null;
                    handler.PlatformView.PrivateImeOptions = "";
    #endif
                });
                ButtonHandler.Mapper.AppendToMapping("Project.Buttons.NoSystemFocus", (handler, view) =>
                {
    #if ANDROID
                    if (view is Button)
                    {
                        handler.PlatformView.FocusableInTouchMode = false;
                        handler.PlatformView.Focusable = false;
                    }
    #endif
                });
    
  2. Windows

Built with Hugo
Theme Stack designed by Jimmy