mr maaak
mr maaak

Reputation: 13

Xamarin.Forms - Why keyboard not hide when switching to another page?

The page has no input fields(only one label), but the keyboard does not disappear when you switch to this page. Is this normal behavior? How to make the keyboard not open on a new page? thank

First page xaml

<ContentPage.Content>
    <StackLayout x:Name="stackLayout2">
        <Label x:Name="code_label" HorizontalOptions="CenterAndExpand" Font="Bold, Micro" TextColor="black" FontSize="19" Margin="15, 30, 15, 0" />
        <Entry x:Name="code_field" TextChanged="CodeFieldTextChanged"  Keyboard="Telephone" MaxLength="4" Margin="15, 30, 15, 0" />
    </StackLayout>
</ContentPage.Content>

First page CodeFieldTextChanged method

private void CodeFieldTextChanged(object sender, TextChangedEventArgs args)
{
    if (args.NewTextValue == "3333")
    {
        Application.Current.MainPage = new HomePage();
    }
}

HomePage xaml

<ContentPage.Content>
    <StackLayout>
        <Label Text="Authorization ok!"
            VerticalOptions="CenterAndExpand" 
            HorizontalOptions="CenterAndExpand" />
    </StackLayout>
</ContentPage.Content>

If Application.Current.MainPage = new HomePage(); is replaсed by Application.Current.MainPage = new NavigationPage(new HomePage()); then keyboard disappears, but in the first case keyboard stays

Upvotes: 0

Views: 850

Answers (1)

jgoldberger - MSFT
jgoldberger - MSFT

Reputation: 6088

This issue only seems to occur on Android. And what is happening is that you are changing the page without ever hitting "Return" (green Check mark on the Telephone keyboard) so the keyboard does not get dismissed by the OS, so you have to dismiss it manually. Try this:

private void CodeFieldTextChanged(object sender, TextChangedEventArgs args)
{
    if (args.NewTextValue == "3333")
    {
        // Add the following two lines to dismiss the keyboard
        Entry entry = sender as Entry;
        entry.Unfocus();
        Application.Current.MainPage = new HomePage();
    }
}

Upvotes: 1

Related Questions