How to prevent Entry Return to move to next control?

axa88 0 Reputation points
2023-10-13T09:57:41.76+00:00

I am using Microsoft.Maui.Controls.Entry object.
I am setting the ReturnType to Send

Entry entry = new() { ReturnType = ReturnType.Send };

So when completing the keyboard entry and using the Return button, i find the focus is sent to the next input control. As if the ReturnType was set to Next rather than Send

The next input control is a Picker object, and forcing the advancement opens the Picker. This is not desired. How can i prevent that behavior?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,906 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 35,966 Reputation points Microsoft Vendor
    2023-10-16T03:09:57.0633333+00:00

    Hello,

    The ReturnType property only provides a difference in button styles and does not affect program behavior.

    Enumerates return button styles. Typically the operating system on-screen keyboard will visually style the return key based on this value.

    After testing, this issue only appears on the Android platform.

    You could refer to the following code to turn off picker's autofocus function.

    <Entry x:Name="myentry"  ReturnType="Send"/>
    <Picker x:Name="testPicker"></Picker>
    
    
    protected override void OnHandlerChanged()
    {
        base.OnHandlerChanged();
    #if ANDROID
        var edit = testPicker.Handler.PlatformView as EditText;
        if (edit != null)
        {
            edit.Focusable = false;
            edit.FocusableInTouchMode = false;
        }
    #endif
    }
    

    Update:

    Since you can't get the control using DataTemplate, you could disable autofocus for all Pickers for this page using the following code.

    void ModifyPicker()
    {
        Microsoft.Maui.Handlers.PickerHandler.Mapper.AppendToMapping("MyCustomization", (handler, view) =>
        {
    #if ANDROID
            var edit = handler.PlatformView as EditText;
            if (edit != null)
            {
                edit.Focusable = false;
                edit.FocusableInTouchMode = false;
            }
    #endif
        });
    }
    
    public MainPage()
    {
        InitializeComponent();
        ModifyPicker();
    }
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.