Skip to content

Commit

Permalink
[Android] Fix ReturnType on Entry (#17546)
Browse files Browse the repository at this point in the history
### Description of Change

Fix **ReturnType** on Android Entry. 

### Issues Fixed

Fixes #17463 

NOTE: It's marked as a regression from NET 7 but I can reproduce it in
NET 8 as well. So I would add it to main and then backport it.
  • Loading branch information
rmarinho authored Oct 4, 2023
2 parents 950374f + 55ed1ea commit a05a5a7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
17 changes: 14 additions & 3 deletions src/Controls/samples/Controls.Sample/Pages/Controls/EntryPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@
Text="Whats new"
IsSpellCheckEnabled="False"
IsTextPredictionEnabled="False"/>
<Label
Text="ReturnType (Search)"
Style="{StaticResource Headline}" />
<Entry
ReturnType="Search" />
<Label
Text="Update the ReturnType just typing"
Style="{StaticResource Headline}" />
<Entry
x:Name="ReturnTypeEntry"
TextChanged="OnReturnTypeEntryTextChanged"/>
<Label
Text="Return Command"
Style="{StaticResource Headline}" />
Expand Down Expand Up @@ -225,9 +236,9 @@
HeightRequest="100"/>
<HorizontalStackLayout>
<Label
x:Name="lblCursor"
Text="CursorPosition = 4"
Style="{StaticResource Headline}" />
x:Name="lblCursor"
Text="CursorPosition = 4"
Style="{StaticResource Headline}" />
<Slider x:Name="sldCursorPosition" ValueChanged="OnSlideCursorPositionValueChanged" WidthRequest="100"/>
</HorizontalStackLayout>
<Entry
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific;
Expand Down Expand Up @@ -99,14 +100,23 @@ void UpdateEntryBackground()
};
}

private void ShowSoftInputAsyncButton_Clicked(object sender, EventArgs e)
void ShowSoftInputAsyncButton_Clicked(object sender, EventArgs e)
{
this.PlaceholderEntryItem.ShowSoftInputAsync(System.Threading.CancellationToken.None);
PlaceholderEntryItem.ShowSoftInputAsync(System.Threading.CancellationToken.None);
}

void HideSoftInputAsyncButton_Clicked(object sender, EventArgs e)
{
PlaceholderEntryItem.HideSoftInputAsync(System.Threading.CancellationToken.None);
}

private void HideSoftInputAsyncButton_Clicked(object sender, EventArgs e)
void OnReturnTypeEntryTextChanged(object sender, TextChangedEventArgs e)
{
this.PlaceholderEntryItem.HideSoftInputAsync(System.Threading.CancellationToken.None);
Random rnd = new Random();
var returnTypeCount = Enum.GetNames(typeof(ReturnType)).Length;
ReturnTypeEntry.ReturnType = (ReturnType)rnd.Next(0, returnTypeCount);

Debug.WriteLine($"ReturnType: {ReturnTypeEntry.ReturnType}");
}
}
}
10 changes: 10 additions & 0 deletions src/Core/src/Handlers/Entry/EntryHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public override void SetVirtualView(IView view)
// TODO: NET8 issoto - Change the return type to MauiAppCompatEditText
protected override void ConnectHandler(AppCompatEditText platformView)
{
platformView.ViewAttachedToWindow += OnViewAttachedToWindow;
platformView.TextChanged += OnTextChanged;
platformView.FocusChange += OnFocusedChange;
platformView.Touch += OnTouch;
Expand All @@ -52,6 +53,7 @@ protected override void DisconnectHandler(AppCompatEditText platformView)
{
_clearButtonDrawable = null;

platformView.ViewAttachedToWindow -= OnViewAttachedToWindow;
platformView.TextChanged -= OnTextChanged;
platformView.FocusChange -= OnFocusedChange;
platformView.Touch -= OnTouch;
Expand Down Expand Up @@ -145,6 +147,14 @@ static void MapFocus(IEntryHandler handler, IEntry entry, object? args)
handler.PlatformView.Focus(request);
}

void OnViewAttachedToWindow(object? sender, ViewAttachedToWindowEventArgs e)
{
if (PlatformView is null || VirtualView is null)
return;

PlatformView.UpdateReturnType(VirtualView);
}

void OnTextChanged(object? sender, TextChangedEventArgs e)
{
if (VirtualView == null)
Expand Down
1 change: 1 addition & 0 deletions src/Core/src/Platform/Android/EditTextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ public static void UpdateClearButtonVisibility(this EditText editText, IEntry en

public static void UpdateReturnType(this EditText editText, IEntry entry)
{
editText.SetInputType(entry);
editText.ImeOptions = entry.ReturnType.ToPlatform();
}

Expand Down

0 comments on commit a05a5a7

Please sign in to comment.