diff --git a/src/Core/src/Platform/Android/EditTextExtensions.cs b/src/Core/src/Platform/Android/EditTextExtensions.cs index 34b0b3edc31a..074b228b8516 100644 --- a/src/Core/src/Platform/Android/EditTextExtensions.cs +++ b/src/Core/src/Platform/Android/EditTextExtensions.cs @@ -370,11 +370,6 @@ internal static void SetInputType(this EditText editText, ITextInput textInput) editText.UpdateIsSpellCheckEnabled(textInput); } - if (keyboard == Keyboard.Numeric) - { - editText.KeyListener = LocalizedDigitsKeyListener.Create(editText.InputType); - } - if (textInput is IEntry entry) { if (entry.IsPassword) @@ -400,6 +395,14 @@ internal static void SetInputType(this EditText editText, ITextInput textInput) if (textInput is IEditor) editText.InputType |= InputTypes.TextFlagMultiLine; + if (keyboard == Keyboard.Numeric) + { + // InputType changes replace the listener; install it last and retain the final flags. + var inputType = editText.InputType; + editText.KeyListener = LocalizedDigitsKeyListener.Create(inputType); + editText.SetRawInputType(inputType); + } + if (textInput is IElement element) { var services = element.Handler?.MauiContext?.Services; diff --git a/src/Core/src/Platform/Android/LocalizedDigitsKeyListener.cs b/src/Core/src/Platform/Android/LocalizedDigitsKeyListener.cs index ca1578918762..7daee89d2d93 100644 --- a/src/Core/src/Platform/Android/LocalizedDigitsKeyListener.cs +++ b/src/Core/src/Platform/Android/LocalizedDigitsKeyListener.cs @@ -92,10 +92,10 @@ protected override char[] GetAcceptedChars() { if ((InputType & InputTypes.NumberFlagSigned) == 0) { - return _acceptedChars ??= new[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', _decimalSeparator }; + return _acceptedChars ??= new[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', _decimalSeparator, '.' }; } - return _acceptedChars ??= new[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', SignCharacter, _decimalSeparator }; + return _acceptedChars ??= new[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', SignCharacter, _decimalSeparator, '.' }; } static bool IsSignChar(char c) @@ -111,8 +111,29 @@ bool IsDecimalPointChar(char c) #pragma warning disable CS8602 // Dereference of a possibly null reference. public override ICharSequence? FilterFormatted(ICharSequence? source, int start, int end, ISpanned? dest, int dstart, int dend) { + // Some numeric IMEs emit '.' even when the locale uses a different separator. + SpannableStringBuilder? localized = null; + if (_decimalSeparator != '.') + { + for (int i = start; i < end; i++) + { + if (source.CharAt(i) == '.') + { + localized ??= new SpannableStringBuilder(source, start, end); + localized.Replace(i - start, i + 1 - start, _decimalSeparator.ToString()); + } + } + } + + if (localized != null) + { + source = localized; + start = 0; + end = localized.Length(); + } + // Borrowed heavily from the Android source - ICharSequence? filterFormatted = base.FilterFormatted(source, start, end, dest, dstart, dend); + ICharSequence? filterFormatted = base.FilterFormatted(source, start, end, dest, dstart, dend) ?? localized; if (filterFormatted != null) { diff --git a/src/Core/tests/DeviceTests/Handlers/Entry/EntryHandlerTests.Android.cs b/src/Core/tests/DeviceTests/Handlers/Entry/EntryHandlerTests.Android.cs index a163bce7aa4a..86709cc9ddaa 100644 --- a/src/Core/tests/DeviceTests/Handlers/Entry/EntryHandlerTests.Android.cs +++ b/src/Core/tests/DeviceTests/Handlers/Entry/EntryHandlerTests.Android.cs @@ -18,6 +18,47 @@ namespace Microsoft.Maui.DeviceTests { public partial class EntryHandlerTests { + [Theory] + [InlineData("sk-SK", false, "3,14")] + [InlineData("sk-SK", true, "3,14")] + [InlineData("pl-PL", false, "3,14")] + [InlineData("en-US", false, "3.14")] + public async Task NumericKeyboardLocalizesPeriodInput(string culture, bool isPassword, string expected) + { + var entry = new EntryStub { Keyboard = Keyboard.Numeric, IsPassword = isPassword }; + + await GetValueAsync(entry, handler => + { + var previousCulture = CultureInfo.CurrentCulture; + try + { + CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(culture); + handler.PlatformView.SetInputType(entry); + using var editorInfo = new EditorInfo(); + using var inputConnection = handler.PlatformView.OnCreateInputConnection(editorInfo); + Assert.NotNull(inputConnection); + + foreach (var text in new[] { "3", ".", "14" }) + { + using var source = new Java.Lang.String(text); + Assert.True(inputConnection.CommitText(source, 1)); + } + + Assert.Equal(expected, handler.PlatformView.Text); + Assert.Equal(expected, entry.Text); + Assert.Equal(expected.Length, handler.PlatformView.SelectionStart); + Assert.True(handler.PlatformView.InputType.HasFlag(InputTypes.NumberFlagDecimal)); + Assert.True(handler.PlatformView.InputType.HasFlag(InputTypes.NumberFlagSigned)); + Assert.Equal(isPassword, handler.PlatformView.InputType.HasFlag(InputTypes.NumberVariationPassword)); + return true; + } + finally + { + CultureInfo.CurrentCulture = previousCulture; + } + }); + } + [Fact(DisplayName = "Numeric keyboard accepts Polish decimal separator")] public async Task NumericKeyboardAcceptsPolishDecimalSeparator() {