0
votes

I want to add focus() to my entry field, When I add the entry.Focus() it's work fine in the entry's in top of the page.But when comes to the bottom it doesn't work smoothly.And also I tried with SoftInput method but it hides the entry in the bottom and also the design is changing when a page has only one or two entry.

Please help me

2
So your problem isn't the focus, but the keyboard hides the entry right ?FabriBertani
Actually I want to focus the next entry, when I press the done in the keyboard option from the previous entry.user10139954

2 Answers

2
votes

The @AbbyWang answer is correct, also you can use the ReturnCommand property.

<StackLayout
    Orientation="Vertical">
    <Entry
        x:Name="firstLabel"
        ReturnType="Next"
        FontSize="Small"
        TextColor="Black" />
    <Entry
        x:Name="secondLabel"
        ReturnType="Done"
        FontSize="Small"
        TextColor="Black" />
</StackLayout>

And the code behind

public YourPage()
{
    InitializeComponent();

    this.firstLabel.ReturnCommand = new Command(() => this.secondLabel.Focus());

    // or you can use this to call a command on your viewmodel
    this.secondLabel.ReturnCommand = new Command(() => YourViewModel.SomeCommand.Execute(null));
}
0
votes

If you just have two Entries on your page, you can simply add a Completed event to your top entry, and call the entry.Focus() for bottom entry in this event. So the code is like this:

MainPage.xaml:

<StackLayout>
        <StackLayout Orientation="Horizontal" VerticalOptions="Start">
            <!-- Place new controls here -->
            <Entry Text="top" x:Name="TopEntry"
            HorizontalOptions="Center"
            VerticalOptions="CenterAndExpand"
            Completed="Entry_Completed"        />
        </StackLayout>


        <StackLayout Orientation="Horizontal" VerticalOptions="End">
            <!-- Place new controls here -->
            <Entry Text="Bottom"  x:Name="BottomEntry"
            HorizontalOptions="Center"
            VerticalOptions="CenterAndExpand" />
        </StackLayout>
</StackLayout>

MainPage.xaml.cs

void Entry_Completed(object sender, EventArgs e)
{
    BottomEntry.Focus();
}

If you have many more than 2 entries and you want to focus on next entry when press the "done" on keyboard, you can also refer to this.