OttoRocket
OttoRocket

Reputation: 13

Keep keyboard open when entry unfocus in iOS .NET Maui

I'm developing an iOS app in .NET Maui and I'm experiencing an issue where the keyboard briefly disappears when I shift focus from one entry field to the next.

By default, if you have two entry fields and press enter in one entry it will not move the focus to the next entry field. To resolve this, I'm using the Completed event to manually move focus to the appropriate field.

This works, but the issue is that in between moving to the next field the keyboard hides and then reappears. This looks janky and interrupts the flow of the app.

Is there a way to keep the keyboard open?

Xaml

<Entry x:Name="entryOne" Completed="Entry_Completed"/>
<Entry x:Name="entryTwo" Completed="Entry_Completed"/>

Codebehind:

private async void Entry_Completed(object sender, EventArgs e)
{
      //Move to next entry field
      if (sender == entryOne)
      entryTwo.Focus();
      else
      entryOne.Focus();
}

Upvotes: 0

Views: 732

Answers (1)

Liqun Shen-MSFT
Liqun Shen-MSFT

Reputation: 7990

You may change the ReturnType property of the Entry control.

From ReturnType Enum, you could set the following value for ReturnType:

Default, Indicates the default style on the platform.

Done, Indicates a "Done" button.

Go, Indicates a "Go" button.

Next, Indicates a "Next" button.

Search, Indicates a "Search" button.

Send, Indicates a "Send" button.

I recommend you use the value of Next. When you click the Next Button, the keyboard will not disappear.

<Entry x:Name="entryOne" Completed="Entry_Completed" ReturnType="Next"/>
<Entry x:Name="entryTwo" Completed="Entry_Completed" ReturnType="Next"/>

Updates

I recommend you use SetFocusOnEntryCompletedBehavior in Maui CommunityToolkit. Here is how to Get started with it.

With SetFocusOnEntryCompletedBehavior, you don't have to set focus event programmatically in code behind. Just try the following code,

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
...
         xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">


<Entry x:Name="entryOne"
       toolkit:SetFocusOnEntryCompletedBehavior.NextElement="{x:Reference entryTwo}"
       ReturnType="Next"
       />
<Entry x:Name="entryTwo"
        toolkit:SetFocusOnEntryCompletedBehavior.NextElement="{x:Reference entryOne}"
        ReturnType="Next"/>

enter image description here

Hope it helps!

Upvotes: 0

Related Questions