maui
maui copied to clipboard
It is not possible to unfocus an Entry and HideSoftInputOnTapped closes soft keyboard but does not remove focus from Entry
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
- Create a new MAUI project
- Replace the main page with an empty page, or empty the main page
- Add an
Entryand optionally give it some Focused visual state styling to make the issue more visible - Add
HideSoftInputOnTapped="True"to theContentPage - Run the app
- Select the
Entryif needed by tapping / clicking on it - Try tapping / clicking outside the
Entryor try pressingtab - See that the
Entrykeeps 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
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
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
@maexsp This worked beautifully. Thank you.
Funcionou muito bem!
Same problem !
@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
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.
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
HideSoftInputOnTappedshould 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.
Duplicate of #11881