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?
Add a comment
|
1 Answer
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.
- Define a custom control that inherits from
Entry. - 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
}
});
}
}
- Call the method (in my case
NoKeyboardEntry.SetHandler();) somewhere. I did it onCreateMauiApp().
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.
5 Comments
Nikola Savić
Your solution worked well. Unfortunately, it does not work with an Editor, just an Entry. I do not mind changing mine Editor into an Entry so I accepted your answer. Thank you.
camionkraken
Just out of curiosity, what does not work with
Editor? I tried and it seems to do the same. Also, the Android native component is the same for both Entry and Editor (AppCompatEditText), so there shouldn't be any difference.Nikola Savić
For me, the keyboard still pops up when I revert to the Editor.
camionkraken
Are you sure you're replacing every occurence of "Entry"? I would say the minimum for it to work is replacing inheritance
: Entry with : Editor and EntryHandler with EditorHandler inside SetHandler(). Maybe did you forgot the last one?Nikola Savić
Oh yeah you are right, I forgot to change the handler.