mauimaui-android

Prevent show SoftKeyboard on entry focused .NET MAUI


I need to hide Softkeyboard when an entry is focused on Net MAUI on .net7 in Android in order to input data from a barcode Scanner.

So far I tried to disable/enable entry when focused, but the behavior is not correct.

I also tried a KeyBoardHelper that I found with the same behavior (not showing cursor or when entry is focused I changed backgroundcolor and with this helper does not change).

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();
            }
        }
    }

Is there other way to prevent to show keyboard when an entry is focused?


Solution

  • In an Entry handler:

    handler.PlatformView.ShowSoftInputOnFocus = false;
    

    Or customize a control on a Page or in MauiProgram using a property mapper:

    Microsoft.Maui.Handlers.EntryHandler.Mapper
      .AppendToMapping("ShowSoftInputOnFocus", (handler, view) =>
    {
      if (view is Entry entry)
      {
    #if ANDROID
        handler.PlatformView.ShowSoftInputOnFocus = false;
    #endif
      }
    });