Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

maui

.net-maui

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.

like image 736
Wal.H Avatar asked Sep 09 '25 16:09

Wal.H


2 Answers

it's more simple than you think :)

private void SingInButton_Clicked(object sender, EventArgs e)
{
    //Trick To Hide VirtualKeyboard
    PasswordEntry.IsEnabled = false;
    PasswordEntry.IsEnabled = true;

}

}

like image 168
devdennis Avatar answered Sep 12 '25 15:09

devdennis


Well, in MAUI there's not such need to create an interface...

Just add in Platforms/Android/KeyboardHelper.cs

using Android.Content;
using Android.View.InputMethods;

namespace ApplicationName.Platforms
{
    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();
            }
        }
    }
}

And in Platforms/iOS/KeyboardHelper.cs

using UIKit;

namespace ApplicationName.Platforms
{
    public static partial class KeyboardHelper
    {
        public static void HideKeyboard()
        {
            UIApplication.SharedApplication.Delegate.GetWindow().EndEditing(true);
        }
    }
}

And that's it.

Then in your application just call:

Platforms.KeyboardHelper.HideKeyboard();

to call the function. The class that will be run depends on the platform.

like image 38
Michele Avatar answered Sep 12 '25 13:09

Michele