From 92193cf6333fffad1a981de6df1576687be09e2c Mon Sep 17 00:00:00 2001 From: Pedro Jesus Date: Tue, 28 Jan 2025 23:36:18 -0300 Subject: [PATCH 1/2] add support to dismiss the prompt with enter on keyboard --- .../AlertManager/AlertManager.Android.cs | 21 ++++++++++++++++++- .../AlertManager/AlertManager.Windows.cs | 3 ++- .../Platform/AlertManager/AlertManager.iOS.cs | 7 +++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/Controls/src/Core/Platform/AlertManager/AlertManager.Android.cs b/src/Controls/src/Core/Platform/AlertManager/AlertManager.Android.cs index 3a843b1fafd7..5a1390ad12d7 100644 --- a/src/Controls/src/Core/Platform/AlertManager/AlertManager.Android.cs +++ b/src/Controls/src/Core/Platform/AlertManager/AlertManager.Android.cs @@ -6,10 +6,12 @@ using Android.Content; using Android.Text; using Android.Views; +using Android.Views.InputMethods; using Android.Widget; using AndroidX.AppCompat.Widget; using Microsoft.Maui.Controls.Internals; using static Android.Views.ViewGroup; +using static Android.Widget.TextView; using AButton = Android.Widget.Button; using AppCompatActivity = AndroidX.AppCompat.App.AppCompatActivity; using AppCompatAlertDialog = AndroidX.AppCompat.App.AlertDialog; @@ -275,8 +277,10 @@ void OnPromptRequested(IView sender, PromptArguments arguments) if (arguments.Keyboard == Keyboard.Numeric) editText.KeyListener = LocalizedDigitsKeyListener.Create(editText.InputType); + editText.EditorAction += OnEditorAction; + if (arguments.MaxLength > -1) - editText.SetFilters(new IInputFilter[] { new InputFilterLengthFilter(arguments.MaxLength) }); + editText.SetFilters([new InputFilterLengthFilter(arguments.MaxLength)]); frameLayout.AddView(editText); alertDialog.SetView(frameLayout); @@ -288,6 +292,21 @@ void OnPromptRequested(IView sender, PromptArguments arguments) alertDialog.Window.SetSoftInputMode(SoftInput.StateVisible); alertDialog.Show(); editText.RequestFocus(); + + void OnEditorAction(object sender, EditorActionEventArgs e) + { + if (sender is not AppCompatEditText editText) + { + return; + } + + editText.EditorAction -= OnEditorAction; + + if (e.ActionId == ImeAction.Done) + { + alertDialog.GetButton((int)DialogButtonType.Positive)?.PerformClick(); + } + } } void UpdateProgressBarVisibility(bool isBusy) diff --git a/src/Controls/src/Core/Platform/AlertManager/AlertManager.Windows.cs b/src/Controls/src/Core/Platform/AlertManager/AlertManager.Windows.cs index 7fd41c517ffd..e9ae8b33b67e 100644 --- a/src/Controls/src/Core/Platform/AlertManager/AlertManager.Windows.cs +++ b/src/Controls/src/Core/Platform/AlertManager/AlertManager.Windows.cs @@ -143,7 +143,8 @@ async void OnPromptRequested(Page sender, PromptArguments arguments) Input = arguments.InitialValue ?? string.Empty, Placeholder = arguments.Placeholder ?? string.Empty, MaxLength = arguments.MaxLength >= 0 ? arguments.MaxLength : 0, - InputScope = arguments.Keyboard.ToInputScope() + InputScope = arguments.Keyboard.ToInputScope(), + DefaultButton = ContentDialogButton.Primary }; if (arguments.Cancel != null) diff --git a/src/Controls/src/Core/Platform/AlertManager/AlertManager.iOS.cs b/src/Controls/src/Core/Platform/AlertManager/AlertManager.iOS.cs index 2fb7db1f05c6..0bdedf20d76d 100644 --- a/src/Controls/src/Core/Platform/AlertManager/AlertManager.iOS.cs +++ b/src/Controls/src/Core/Platform/AlertManager/AlertManager.iOS.cs @@ -136,6 +136,13 @@ void PresentPrompt(Page sender, PromptArguments arguments) uiTextField.Text = arguments.InitialValue; uiTextField.ShouldChangeCharacters = (field, range, replacementString) => arguments.MaxLength <= -1 || field.Text.Length + replacementString.Length - range.Length <= arguments.MaxLength; uiTextField.ApplyKeyboard(arguments.Keyboard); + uiTextField.ShouldReturn = _ => + { + arguments.SetResult(alert.TextFields[0].Text); + alert.DismissViewController(true, null); + + return false; + }; }); var oldFrame = alert.View.Frame; From b22d51fbb3b22235cb4acc13c637176faf2336f9 Mon Sep 17 00:00:00 2001 From: Gerald Versluis Date: Tue, 12 Aug 2025 17:32:57 +0200 Subject: [PATCH 2/2] Windows only --- .../AlertManager/AlertManager.Android.cs | 21 +------------------ .../Platform/AlertManager/AlertManager.iOS.cs | 7 ------- 2 files changed, 1 insertion(+), 27 deletions(-) diff --git a/src/Controls/src/Core/Platform/AlertManager/AlertManager.Android.cs b/src/Controls/src/Core/Platform/AlertManager/AlertManager.Android.cs index 5a1390ad12d7..3a843b1fafd7 100644 --- a/src/Controls/src/Core/Platform/AlertManager/AlertManager.Android.cs +++ b/src/Controls/src/Core/Platform/AlertManager/AlertManager.Android.cs @@ -6,12 +6,10 @@ using Android.Content; using Android.Text; using Android.Views; -using Android.Views.InputMethods; using Android.Widget; using AndroidX.AppCompat.Widget; using Microsoft.Maui.Controls.Internals; using static Android.Views.ViewGroup; -using static Android.Widget.TextView; using AButton = Android.Widget.Button; using AppCompatActivity = AndroidX.AppCompat.App.AppCompatActivity; using AppCompatAlertDialog = AndroidX.AppCompat.App.AlertDialog; @@ -277,10 +275,8 @@ void OnPromptRequested(IView sender, PromptArguments arguments) if (arguments.Keyboard == Keyboard.Numeric) editText.KeyListener = LocalizedDigitsKeyListener.Create(editText.InputType); - editText.EditorAction += OnEditorAction; - if (arguments.MaxLength > -1) - editText.SetFilters([new InputFilterLengthFilter(arguments.MaxLength)]); + editText.SetFilters(new IInputFilter[] { new InputFilterLengthFilter(arguments.MaxLength) }); frameLayout.AddView(editText); alertDialog.SetView(frameLayout); @@ -292,21 +288,6 @@ void OnPromptRequested(IView sender, PromptArguments arguments) alertDialog.Window.SetSoftInputMode(SoftInput.StateVisible); alertDialog.Show(); editText.RequestFocus(); - - void OnEditorAction(object sender, EditorActionEventArgs e) - { - if (sender is not AppCompatEditText editText) - { - return; - } - - editText.EditorAction -= OnEditorAction; - - if (e.ActionId == ImeAction.Done) - { - alertDialog.GetButton((int)DialogButtonType.Positive)?.PerformClick(); - } - } } void UpdateProgressBarVisibility(bool isBusy) diff --git a/src/Controls/src/Core/Platform/AlertManager/AlertManager.iOS.cs b/src/Controls/src/Core/Platform/AlertManager/AlertManager.iOS.cs index 0bdedf20d76d..2fb7db1f05c6 100644 --- a/src/Controls/src/Core/Platform/AlertManager/AlertManager.iOS.cs +++ b/src/Controls/src/Core/Platform/AlertManager/AlertManager.iOS.cs @@ -136,13 +136,6 @@ void PresentPrompt(Page sender, PromptArguments arguments) uiTextField.Text = arguments.InitialValue; uiTextField.ShouldChangeCharacters = (field, range, replacementString) => arguments.MaxLength <= -1 || field.Text.Length + replacementString.Length - range.Length <= arguments.MaxLength; uiTextField.ApplyKeyboard(arguments.Keyboard); - uiTextField.ShouldReturn = _ => - { - arguments.SetResult(alert.TextFields[0].Text); - alert.DismissViewController(true, null); - - return false; - }; }); var oldFrame = alert.View.Frame;