需要一种方法在 MAUI 的编辑器/输入字段中隐藏软键盘

Wal*_*l.H 16 maui .net-maui

我发现此链接中似乎有用的内容:

Xamarin Forms 中的键盘禁用了 Entry 控件

但它似乎只适用于 Xamarin Forms。我什至在我的 MAUI 应用程序中使用了它,但它根本没有效果!

我希望这样做的原因是因为我想启用编辑器字段的焦点,但不触发软键盘(对于条形码扫描仪字段)

谢谢。

小智 21

它比你想象的更简单:)

private void SingInButton_Clicked(object sender, EventArgs e)
{
    //Trick To Hide VirtualKeyboard
    PasswordEntry.IsEnabled = false;
    PasswordEntry.IsEnabled = true;

}
Run Code Online (Sandbox Code Playgroud)

}

  • 确实更简单了,谢谢 (2认同)

小智 13

  • 要在 MAUI 中显示软键盘,请将焦点设置为可编辑控件。

  • 要隐藏 MAUI 中的软键盘,请从可编辑控件中删除焦点。您可以简单地通过代码或当用户单击按钮时移动焦点。

  • 上述行为在 Xamarin Forms 中运行良好,但目前在 MAUI 中存在 BUG。一旦显示软键盘,它就不会隐藏。

  • 目前解决此问题的方法是禁用编辑控件然后启用它,这将隐藏键盘,代码片段如下:

    this.DescriptionEditor.IsEnabled = false; this.DescriptionEditor.IsEnabled = true;

请参阅下面的链接: 如何在 Xamarin Forms 中按下按钮时关闭键盘


小智 11

嗯,在 MAUI 中没有必要创建一个界面......

只需添加 Platforms/Android/KeyboardHelper.cs

namespace ApplicationName.Platforms
{
    public static partial class KeyboardHelper
    {
        public static void HideKeyboard()
        {
            var context = Platform.AppContext;
            var inputMethodManager = context.GetSystemService(Context.InputMethodService) as InputMethodManager;
            if (inputMethodManager != null)
            {
                var activity = Platform.CurrentActivity;
                var token = activity.CurrentFocus?.WindowToken;
                inputMethodManager.HideSoftInputFromWindow(token, HideSoftInputFlags.None);
                activity.Window.DecorView.ClearFocus();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在 Platforms/iOS/KeyboardHelper.cs 中

namespace ApplicationName.Platforms
{
    public static partial class KeyboardHelper
    {
        public static void HideKeyboard()
        {
            UIApplication.SharedApplication.KeyWindow.EndEditing(true);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

就是这样。

然后在您的应用程序中只需调用:

Platforms.KeyboardHelper.HideKeyboard();
Run Code Online (Sandbox Code Playgroud)

来调用该函数。将运行的类取决于平台。