mauieditorandroid-softkeyboard

How to disable keyboard from appearing while allowing selection on an Editor in .NET MAUI?


I have an Editor in MAUI that as of now allows for tapping but each time I tap, it opens up the keyboard (on Android). I do want to be able to tap so that I can change cursor position but don't want for keyboard to appear at all. All the inputs that are accepted are through buttons and I programatically update the Editor.

I tried using KeyboardExtensions.HideKeyboardAsync(https://learn.microsoft.com/en-us/dotnet/communitytoolkit/maui/extensions/keyboard-extensions), TapGestureRecognizer and Activity(https://github.com/dotnet/maui/discussions/4907), Editor.InputTransparent, ContentPage.HideSoftInputOnTapped(https://docs.telerik.com/devtools/maui/knowledge-base/hide-softkeyboard-without-losing-focus-maui-entry), Option 2 on the same website(https://docs.telerik.com/devtools/maui/knowledge-base/hide-softkeyboard-without-losing-focus-maui-entry).

These methods end up fully disabling the ability to access the Editor so none od them solved the problem. Im looking for a general approach (any platform) or at least something Android-specific. Is there a way to hide the keyboard while mainting the cursor position?


Solution

  • I think that for this kind of customization you have to use handlers.

    I tried something like this and it seems to work for Android. You should probably organize it better than I did... it was just a PoC.

    1. Define a custom control that inherits from Entry.
    2. Define a method that customize the handler for your custom control.

    NoKeyboardEntry.cs

    namespace YourApp;
    
    public class NoKeyboardEntry : Entry
    {
        public static void SetHandler()
        {
            Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("NoKeyboardEntryCustomization", (handler, view) =>
            {
                if (view is NoKeyboardEntry)
                {
    #if ANDROID
                    handler.PlatformView.ShowSoftInputOnFocus = false;
    #endif
                }
            });
        }
    }
    
    1. Call the method (in my case NoKeyboardEntry.SetHandler();) somewhere. I did it on CreateMauiApp().

    Edit

    I realized you need an Editor and not an Entry. The procedure should be the same, just replace all the occurrences of "Entry" with "Editor" and you should be ready to go.