Skip to content

Commit 92193cf

Browse files
pictosjfversluis
authored andcommitted
add support to dismiss the prompt with enter on keyboard
1 parent 7084b9f commit 92193cf

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

src/Controls/src/Core/Platform/AlertManager/AlertManager.Android.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
using Android.Content;
77
using Android.Text;
88
using Android.Views;
9+
using Android.Views.InputMethods;
910
using Android.Widget;
1011
using AndroidX.AppCompat.Widget;
1112
using Microsoft.Maui.Controls.Internals;
1213
using static Android.Views.ViewGroup;
14+
using static Android.Widget.TextView;
1315
using AButton = Android.Widget.Button;
1416
using AppCompatActivity = AndroidX.AppCompat.App.AppCompatActivity;
1517
using AppCompatAlertDialog = AndroidX.AppCompat.App.AlertDialog;
@@ -275,8 +277,10 @@ void OnPromptRequested(IView sender, PromptArguments arguments)
275277
if (arguments.Keyboard == Keyboard.Numeric)
276278
editText.KeyListener = LocalizedDigitsKeyListener.Create(editText.InputType);
277279

280+
editText.EditorAction += OnEditorAction;
281+
278282
if (arguments.MaxLength > -1)
279-
editText.SetFilters(new IInputFilter[] { new InputFilterLengthFilter(arguments.MaxLength) });
283+
editText.SetFilters([new InputFilterLengthFilter(arguments.MaxLength)]);
280284

281285
frameLayout.AddView(editText);
282286
alertDialog.SetView(frameLayout);
@@ -288,6 +292,21 @@ void OnPromptRequested(IView sender, PromptArguments arguments)
288292
alertDialog.Window.SetSoftInputMode(SoftInput.StateVisible);
289293
alertDialog.Show();
290294
editText.RequestFocus();
295+
296+
void OnEditorAction(object sender, EditorActionEventArgs e)
297+
{
298+
if (sender is not AppCompatEditText editText)
299+
{
300+
return;
301+
}
302+
303+
editText.EditorAction -= OnEditorAction;
304+
305+
if (e.ActionId == ImeAction.Done)
306+
{
307+
alertDialog.GetButton((int)DialogButtonType.Positive)?.PerformClick();
308+
}
309+
}
291310
}
292311

293312
void UpdateProgressBarVisibility(bool isBusy)

src/Controls/src/Core/Platform/AlertManager/AlertManager.Windows.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ async void OnPromptRequested(Page sender, PromptArguments arguments)
143143
Input = arguments.InitialValue ?? string.Empty,
144144
Placeholder = arguments.Placeholder ?? string.Empty,
145145
MaxLength = arguments.MaxLength >= 0 ? arguments.MaxLength : 0,
146-
InputScope = arguments.Keyboard.ToInputScope()
146+
InputScope = arguments.Keyboard.ToInputScope(),
147+
DefaultButton = ContentDialogButton.Primary
147148
};
148149

149150
if (arguments.Cancel != null)

src/Controls/src/Core/Platform/AlertManager/AlertManager.iOS.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ void PresentPrompt(Page sender, PromptArguments arguments)
136136
uiTextField.Text = arguments.InitialValue;
137137
uiTextField.ShouldChangeCharacters = (field, range, replacementString) => arguments.MaxLength <= -1 || field.Text.Length + replacementString.Length - range.Length <= arguments.MaxLength;
138138
uiTextField.ApplyKeyboard(arguments.Keyboard);
139+
uiTextField.ShouldReturn = _ =>
140+
{
141+
arguments.SetResult(alert.TextFields[0].Text);
142+
alert.DismissViewController(true, null);
143+
144+
return false;
145+
};
139146
});
140147

141148
var oldFrame = alert.View.Frame;

0 commit comments

Comments
 (0)