maui icon indicating copy to clipboard operation
maui copied to clipboard

It is not possible to unfocus an Entry and HideSoftInputOnTapped closes soft keyboard but does not remove focus from Entry

Open hansmbakker opened this issue 1 year ago • 6 comments
trafficstars

Description

In general it seems not possible to unfocus an Entry by using the tab key on windows or by clicking somewhere in the page on Windows or by tapping somewhere on the page after adding HideSoftInputOnTapped="True" to my ContentPages. The latter only makes the soft keyboard close when I tap outside an Entry.

Actual behavior: the Entry stays focused after pressing tab / tapping outside the Entry. The Entry keeps displaying a cursor and it keeps the Focused visual state.

Expected behavior: the Entry loses focus after pressing tab / tapping outside the Entry. It removes the cursor and it returns to the Normal visual state.

Steps to Reproduce

  1. Create a new MAUI project
  2. Replace the main page with an empty page, or empty the main page
  3. Add an Entry and optionally give it some Focused visual state styling to make the issue more visible
  4. Add HideSoftInputOnTapped="True" to the ContentPage
  5. Run the app
  6. Select the Entry if needed by tapping / clicking on it
  7. Try tapping / clicking outside the Entry or try pressing tab
  8. See that the Entry keeps focused

Link to public reproduction project repository

https://github.com/hansmbakker/bugrepro-no-unfocus

Version with bug

8.0.7 SR2

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android, Windows

Affected platform versions

No response

Did you find any workaround?

No

Relevant log output

No response

hansmbakker avatar Mar 06 '24 14:03 hansmbakker

Hi.

Experienced the same problem on Android (iOS works already well out of the box). You can solve this to unfocus and commit the value on an Entry on touch ouside by checking the coordinates within the screen. Works also when the app is in split-mode or popup-window-mode! Most people try to use Android API GetGlobalVisibleRect(outRect) but in MAUI that seems not to work at all. Always returns 0,0,0,0 rect.

Put this into the MAUI Android MainActivity.cs

public override bool DispatchTouchEvent(MotionEvent? e)
{
    if (e!.Action == MotionEventActions.Down)
    {
        var focusedElement = CurrentFocus;
        if (focusedElement is EditText editText)
        {
            var editTextLocation = new int[2];
            editText.GetLocationOnScreen(editTextLocation);
            var clearTextButtonWidth = 100; // syncfusion clear button at the end of the control
            var editTextRect = new Rect(editTextLocation[0], editTextLocation[1], editText.Width + clearTextButtonWidth, editText.Height);
            //var editTextRect = editText.GetGlobalVisibleRect(editTextRect);  //not working in MAUI, always returns 0,0,0,0
            var touchPosX = (int)e.RawX;
            var touchPosY = (int)e.RawY;
            if (!editTextRect.Contains(touchPosX, touchPosY))
            {
                editText.ClearFocus();
                var inputService = GetSystemService(Context.InputMethodService) as InputMethodManager;
                inputService?.HideSoftInputFromWindow(editText.WindowToken, 0);
            }
        }
    }
    return base.DispatchTouchEvent(e);
}

BR, Markus

maexsp avatar Mar 14 '24 10:03 maexsp

Verified this on VS 17.10.0 Preview 2.0(8.0.14). Repro on Windows 11, Android 14.0-API34, MacCatalyst, not repro on iOS 17.2 with below Project: 21053.zip

XamlTest avatar Apr 08 '24 07:04 XamlTest

@maexsp This worked beautifully. Thank you.

MrFrawsty avatar Apr 13 '24 13:04 MrFrawsty

Funcionou muito bem!

LeoLamego avatar Apr 20 '24 16:04 LeoLamego

Same problem !

PauchardThomas avatar Apr 23 '24 09:04 PauchardThomas

@maexsp works like a charm. Thank you! Apparently, the compatibility controls call the HideSoftInputFromWindow when un-focusing. https://github.com/dotnet/maui/blob/222ca3ca088d199ff22adde8ca3ae82d9dba0b35/src/Core/src/Platform/Android/ViewExtensions.cs#L713 However Maui controls backed by an EditText show the soft input when focusing, but never hides it when the Element is unfocusing. See https://github.com/dotnet/maui/blob/222ca3ca088d199ff22adde8ca3ae82d9dba0b35/src/Core/src/Platform/Android/TextViewExtensions.cs#L128-L138

tharindup avatar May 03 '24 12:05 tharindup

https://github.com/dotnet/maui/issues/11881 https://github.com/dotnet/maui/issues/18854 https://github.com/dotnet/maui/issues/19062 https://github.com/dotnet/maui/issues/19032 https://github.com/dotnet/maui/issues/15681

This issues are related same, when we click outside of entry, it is not unfocusing and event also not firing, Still MAUI team not fixing this issue. Simply closing all issues. This issue also will closed with proper solution from MAUI Team Issue happen on latest Android 14 devices as well.

letscodewithkalyan avatar May 08 '24 10:05 letscodewithkalyan

Hi! We understand that this was a behavior change from Xamarin.Forms, but it was an intentional one. We do not consider this to be a bug.

Please see this comment from Rachel on #11881.

Hi All,

In developing .NET MAUI, we learned that it is not possible on earlier versions of Android to unfocus an entry. Some control must always be focused. In Xamarin.Forms, this was approached by setting focus on the page layout; unfortunately, this approach created major accessibility issues. For these reasons, .NET MAUI no longer allows for this behavior by default.

If this antipattern can be avoided, we highly recommend using a different approach. If it cannot be avoided, we'd love to learn more about your scenario so that we can better understand your development needs! That being said, we do understand the needs from a migration compatibility standpoint, so for those scenarios, there is a new property that was introduced in .NET 8. Using HideSoftInputOnTapped should do the trick.

.NET MAUI aims to be as accessible as possible with little interaction from developers, so we will not revert this behavior. Thank you for understanding.

samhouts avatar May 17 '24 01:05 samhouts

Duplicate of #11881

samhouts avatar May 17 '24 01:05 samhouts