.NET MAUI移动应用程序:强制显示屏幕键盘

huangapple go评论82阅读模式
英文:

.NET MAUI mobile app: Force on-screen keyboard display

问题

在我的.NET MAUI移动应用中,支持外部NFC读卡器。当连接时,外部NFC读卡器会像外部键盘一样,将识别的数据输入到焦点的可视元素中(例如可编辑的输入框)。

问题是,当外部NFC读卡器连接时,当我点击可编辑字段时,屏幕上不再显示虚拟键盘。显然,设备认为我已经连接了一个"键盘",因此不再需要屏幕上的虚拟键盘了。

有没有一种方法可以强制显示屏幕上的虚拟键盘?

英文:

In my .NET MAUI mobile app, there's support for external NFC readers. When connected, the external NFC reader acts like an external keyboard, typing the recognized data into the focused visual element (e.g. editable entry).

The problem is that when the external NFC reader is connected, the on-screen keyboard is no longer displayed when I tap on an editable field. Apparently, the device decides that I already have a "keyboard" connected and there's no need for an on-screen keyboard anymore.

Is there a way to force the on-screen keyboard to be displayed?

答案1

得分: 1

对于 Android,您可以在 Entry 的 focused 事件中使用 InputMethodManager 来显示软键盘。例如:

private void Entry_Focused(object sender, FocusEventArgs e)
{
#if ANDROID
    Android.Views.InputMethods.InputMethodManager inputManager = (Android.Views.InputMethods.InputMethodManager)(Microsoft.Maui.ApplicationModel.Platform.CurrentActivity).GetSystemService(Android.App.Application.InputMethodService);
    inputManager.ShowSoftInput((sender as Entry).Handler.PlatformView as AndroidX.AppCompat.Widget.AppCompatEditText, Android.Views.InputMethods.ShowFlags.Forced);
    //inputManager.ToggleSoftInput(Android.Views.InputMethods.InputMethodManager.ShowForced, Android.Views.InputMethods.HideSoftInputFlags.ImplicitOnly);
    // ToggleSoftInput 方法在 API 级别 31 中已被弃用,但仍然有效
#endif
}

但是在 iOS 上没有 API 可以强制显示软键盘。用户需要自行在设备系统设置中更改输入设置。

英文:

For the android, you can use the InputMethodManager in the Entry's focused event to make the soft keyboard show. Such as;

    private void Entry_Focused(object sender, FocusEventArgs e)
    {
#if ANDROID
        Android.Views.InputMethods.InputMethodManager inputManager = (Android.Views.InputMethods.InputMethodManager)(Microsoft.Maui.ApplicationModel.Platform.CurrentActivity).GetSystemService(Android.App.Application.InputMethodService);
        inputManager.ShowSoftInput((sender as Entry).Handler.PlatformView as AndroidX.AppCompat.Widget.AppCompatEditText, Android.Views.InputMethods.ShowFlags.Forced);
        //inputManager.ToggleSoftInput(Android.Views.InputMethods.InputMethodManager.ShowForced, Android.Views.InputMethods.HideSoftInputFlags.ImplicitOnly);
        // the ToggleSoftInput method was deprecated in API level 31, but it still works
#endif
    }

But there is no api for the ios can force the soft keyboard show. User needs to changed the input settings in the device system setting by themself.

huangapple
  • 本文由 发表于 2023年7月20日 17:01:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728271.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定