From ed26340b0ee2cb5e5f4bc04159967ac405b7a092 Mon Sep 17 00:00:00 2001 From: Joe Manke Date: Wed, 10 Jul 2019 13:18:42 -0400 Subject: [PATCH] Auto-focus next view from Entry with ReturnType.Next (#6706) * Automatically focus the next view after hitting the Return key on an Entry with ReturnType.Next * UWP implementation --- .../EntryReturnTypeGalleryPage.cs | 17 ++++++++++++++--- .../Renderers/EntryRenderer.cs | 22 ++++++++++++++++++++-- Xamarin.Forms.Platform.UAP/EntryRenderer.cs | 12 +++++++++--- .../Renderers/EntryRenderer.cs | 6 ++++++ 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/Xamarin.Forms.Controls/ControlGalleryPages/EntryReturnTypeGalleryPage.cs b/Xamarin.Forms.Controls/ControlGalleryPages/EntryReturnTypeGalleryPage.cs index 01611ee..5ee9fbd 100644 --- a/Xamarin.Forms.Controls/ControlGalleryPages/EntryReturnTypeGalleryPage.cs +++ b/Xamarin.Forms.Controls/ControlGalleryPages/EntryReturnTypeGalleryPage.cs @@ -3,11 +3,13 @@ using System.Threading.Tasks; namespace Xamarin.Forms.Controls { - public class EntryReturnTypeGalleryPage: ContentPage + public class EntryReturnTypeGalleryPage : ContentPage { Picker picker; Entry returnTypeEntry; Label lblCompleted; + Entry nextEntry; + public EntryReturnTypeGalleryPage() { BackgroundColor = Color.LightBlue; @@ -35,7 +37,7 @@ namespace Xamarin.Forms.Controls { HorizontalOptions = LayoutOptions.Fill, Placeholder = $"Entry with {ReturnType.Go}", - ReturnCommand = new Command(obj => + ReturnCommand = new Command(obj => { lblCompleted.Text = "Completed Fired"; }), @@ -48,6 +50,7 @@ namespace Xamarin.Forms.Controls if (e.PropertyName == Entry.ReturnTypeProperty.PropertyName) { returnTypeEntry.Placeholder = $"Entry with {returnTypeEntry.ReturnType}"; + lblCompleted.Text = null; } }; @@ -79,9 +82,17 @@ namespace Xamarin.Forms.Controls } }; - layout.Children.Add(returnTypeEntry); + nextEntry = new Entry + { + Placeholder = "Next Entry to Focus", + ReturnType = ReturnType.Next, + HorizontalOptions = LayoutOptions.FillAndExpand + }; + layout.Children.Add(picker); + layout.Children.Add(returnTypeEntry); layout.Children.Add(lblCompleted); + layout.Children.Add(nextEntry); picker.SelectedIndex = 0; Content = layout; diff --git a/Xamarin.Forms.Platform.Android/Renderers/EntryRenderer.cs b/Xamarin.Forms.Platform.Android/Renderers/EntryRenderer.cs index 75acb2e..dfc5230 100644 --- a/Xamarin.Forms.Platform.Android/Renderers/EntryRenderer.cs +++ b/Xamarin.Forms.Platform.Android/Renderers/EntryRenderer.cs @@ -91,8 +91,26 @@ namespace Xamarin.Forms.Platform.Android // Fire Completed and dismiss keyboard for hardware / physical keyboards if (actionId == ImeAction.Done || actionId == _currentInputImeFlag || (actionId == ImeAction.ImeNull && e.KeyCode == Keycode.Enter && e.Action == KeyEventActions.Up)) { - EditText.ClearFocus(); - v.HideKeyboard(); + global::Android.Views.View nextFocus = null; + if (_currentInputImeFlag == ImeAction.Next) + { + nextFocus = FocusSearch(v, FocusSearchDirection.Forward); + } + + if (nextFocus != null) + { + nextFocus.RequestFocus(); + if (!nextFocus.OnCheckIsTextEditor()) + { + v.HideKeyboard(); + } + } + else + { + EditText.ClearFocus(); + v.HideKeyboard(); + } + ((IEntryController)Element).SendCompleted(); } diff --git a/Xamarin.Forms.Platform.UAP/EntryRenderer.cs b/Xamarin.Forms.Platform.UAP/EntryRenderer.cs index 27cbd46..cbe8513 100644 --- a/Xamarin.Forms.Platform.UAP/EntryRenderer.cs +++ b/Xamarin.Forms.Platform.UAP/EntryRenderer.cs @@ -165,9 +165,15 @@ namespace Xamarin.Forms.Platform.UWP if (args?.Key != VirtualKey.Enter) return; - - // Hide the soft keyboard; this matches the behavior of Forms on Android/iOS - Windows.UI.ViewManagement.InputPane.GetForCurrentView().TryHide(); + if (Element.ReturnType == ReturnType.Next) + { + FocusManager.TryMoveFocus(FocusNavigationDirection.Next); + } + else + { + // Hide the soft keyboard; this matches the behavior of Forms on Android/iOS + Windows.UI.ViewManagement.InputPane.GetForCurrentView().TryHide(); + } ((IEntryController)Element).SendCompleted(); } diff --git a/Xamarin.Forms.Platform.iOS/Renderers/EntryRenderer.cs b/Xamarin.Forms.Platform.iOS/Renderers/EntryRenderer.cs index 118e59a..3db72c9 100644 --- a/Xamarin.Forms.Platform.iOS/Renderers/EntryRenderer.cs +++ b/Xamarin.Forms.Platform.iOS/Renderers/EntryRenderer.cs @@ -231,6 +231,12 @@ namespace Xamarin.Forms.Platform.iOS { Control.ResignFirstResponder(); ((IEntryController)Element).SendCompleted(); + + if (Element.ReturnType == ReturnType.Next) + { + FocusSearch(true); + } + return false; } -- 2.7.4