SJaka
SJaka

Reputation: 830

Maui - How to capture backspace key and move between Entry controls

For my project, I'm making a security unlock pincode. It has a set of 4 <Entry> controls in which I can input the security pincode using keyboard.

I've to capture the backspace key (if pressed by the user) and move back between individual <Entry> components. How can I capture the backspace keypress and then move between the Entry screens for both Android and iOS?

Upvotes: 1

Views: 268

Answers (1)

You can use Entry.Focus() method to move between entries you have, I know you can capture backspace key in Xamarin but not sure how you can do it in MAUI, you could read this similar post using handlers How can I detect keyboard backspace key in NET.MAUI (both Android & iOS implementations)?

This is working only with one digit format an placing a behavior to limit your entry:

XAML

          <HorizontalStackLayout>
            <Entry
                x:Name="entry1"
                FontSize="Title"
                Keyboard="Numeric"
                TextChanged="entry1_TextChanged"/>
            <Entry
                x:Name="entry2"
                FontSize="Title"
                Keyboard="Numeric"
                TextChanged="entry2_TextChanged"/>
            <Entry
                x:Name="entry3"
                FontSize="Title"
                Keyboard="Numeric"
                TextChanged="entry3_TextChanged"/>
            <Entry
                x:Name="entry4"
                FontSize="Title"
                Keyboard="Numeric"
                TextChanged="entry4_TextChanged"/>
        </HorizontalStackLayout>

CS

private void entry1_TextChanged(object sender, TextChangedEventArgs e)
{
    if (string.IsNullOrEmpty(e.OldTextValue))
    {
        entry2.Focus();
    }
}

private void entry2_TextChanged(object sender, TextChangedEventArgs e)
{
    if (string.IsNullOrEmpty(e.OldTextValue))
    {
        entry3.Focus();
    }
    else
    {
        entry1.Focus();
    }
}

private void entry3_TextChanged(object sender, TextChangedEventArgs e)
{
    if (string.IsNullOrEmpty(e.OldTextValue))
    {
        entry4.Focus();
    }
    else
    {
        entry2.Focus();
    }
}

private void entry4_TextChanged(object sender, TextChangedEventArgs e)
{
    if (!string.IsNullOrEmpty(e.OldTextValue))
    {
        entry3.Focus();
    }
}

Upvotes: 1

Related Questions