Need a way to hide soft keyboard in MAUI's Editor / Entry fields

Viewed 513

I found what seems to be useful in this link:

A Keyboard disabled Entry control in Xamarin Forms

But it seems that it only works in Xamarin Forms. I even used it in my MAUI app, but it simply has no effect !

The reason I am looking to do this is because I want to enable focus on the Editor field but without triggering the soft keyboard (for a barcode scanner field)

Thanks.

3 Answers

There are PRs to implement the feature. You can refer to this thread.

You can use Focus() method to enable focus on the Editor field but without triggering the soft keyboard on Android.

Here is the code sample below for your reference:

Code in Xaml:

    <Entry x:Name="entry1"  Placeholder="please input" />

Backend Code:

    private void Button_Clicked(object sender, EventArgs e)
    {
        entry1.Focus();
    }

Update:

While there are many benefits to using the new handler-mapper pattern, it's still possible to use the custom renderer in MAUI.Please refer to this thread.

  • To show the soft keyboard in MAUI set the focus to an editable control.

  • To hide the soft keyboard in MAUI remove the focus from the editable control. You can simply move the focus by code or when the user clicks on a button.

  • The above behaviour works fine in Xamarin Forms, but there is a BUG in MAUI for now. Once the soft keyboard is hown, it does not get hidden.

  • The work around this issue for now is to disable the edit control then enable it, this will hide the keyboard, a code snippet is given below: this.DescriptionEditor.IsEnabled = false; this.DescriptionEditor.IsEnabled = true;

    See the link below: How to dismiss keyboard on button press in Xamarin Forms

To solve this i made the following (using this (reference that said alexandar) & this):

  1. On MauiProgram add after builder:

     builder
                 .UseMauiCompatibility()
                 .ConfigureMauiHandlers((handlers) => {
     #if ANDROID
                             handlers.AddCompatibilityRenderer(typeof(yournamespaceofhelper.SoftkeyboardDisabledEntry),typeof(yournamespaceonAndroid.SoftkeyboardDisabledEntryRenderer)); #endif})
    

2.Create a helper (which it's the one you will use it with disabled keyboard):

public class SoftkeyboardDisabledEntry : Entry
{

}
  1. On Platforms/Android create the renderer

[assembly: ExportRenderer(typeof(SoftkeyboardDisabledEntry), typeof(SoftkeyboardDisabledEntryRenderer))] namespace YourAppName.Platforms.Android { public class SoftkeyboardDisabledEntryRenderer : EntryRenderer { public SoftkeyboardDisabledEntryRenderer(Context context) : base(context) { }

    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if (e.NewElement != null)
        {
            ((SoftkeyboardDisabledEntry)e.NewElement).PropertyChanging += OnPropertyChanging;
        }

        if (e.OldElement != null)
        {
            ((SoftkeyboardDisabledEntry)e.OldElement).PropertyChanging -= OnPropertyChanging;
        }

        // Disable the Keyboard on Focus
        this.Control.ShowSoftInputOnFocus = false;
    }

    private void OnPropertyChanging(object sender, PropertyChangingEventArgs propertyChangingEventArgs)
    {
        // Check if the view is about to get Focus
        if (propertyChangingEventArgs.PropertyName == VisualElement.IsFocusedProperty.PropertyName)
        {
            // incase if the focus was moved from another Entry
            // Forcefully dismiss the Keyboard 
            InputMethodManager imm = (InputMethodManager)this.Context.GetSystemService(Context.InputMethodService);
            imm.HideSoftInputFromWindow(this.Control.WindowToken, 0);
        }
    }

    }
}

How this work? you will use a custom entry, that everytime you write something on the entry it's get unfocused, doing this, when the property of the entry changes, it will keep what you write on it but dissappear with the focus / unfocus. The big con of this it's that if you use (for example in my case a magnetic card reader) the reader write so fast so the entry get's focused / unfocused and the keyboard appears. working on it to make a delay so the keyboard doesn't show to the user.

Answering the question you said to alexandar, in case you touched the editor, the keyboard will stay hidden

PD: I couldn't find a way to make the code readable so if someone can edit it, it is appreciated

Related