Home > Blockchain >  Xamarin android focus change from Picker to Entry does not open Keyboard
Xamarin android focus change from Picker to Entry does not open Keyboard

Time:12-07

I have a bunch of Entrys and Picker and I want the focus to automatically change to the next Entry/Picker. Im doing that by calling the Focus() method on the Entry/Picker I wanna focus to. If my current focus is on an Entry and my next element is an Entry or a Picker it changes the focus and opens the Keyboard/Pickerdialog as it's supposed to. If my current focus is on a Picker and the next element is an Entry then the Entry gets focused, as supposed to, but the keyboard does not open. The focus() method is called in the Pickers SelectedIndexChanged event. How can I fix that?

xaml code:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:d="http://xamarin.com/schemas/2014/forms/design"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d"
         x:Class="ABM.Ablesegeraet.Views.TourenLadenPage"
         Title="Touren laden">
<ContentPage.Content>
    <StackLayout>
    <Label Grid.Row="0" Grid.Column="0" Margin="30,10,0,0" FontSize="Medium" Text="Mandant:"/>
    <Picker x:Name="MandantenPicker" ItemDisplayBinding="{Binding Ort}" Title="Mandant" Grid.Row="0" Grid.Column="1" SelectedIndexChanged="MandantPicker_SelectedIndexChanged"/>
    <Entry Grid.Row="1" x:Name="test"/>
    </StackLayout>
</ContentPage.Content>

SelectedIndexcChanged:

private void MandantPicker_SelectedIndexChanged(object sender, EventArgs e)
    {
        test.Focus();
    }

CodePudding user response:

Picker did not lose focus during the SelectedIndexChanged method of Picker, and then if you set Entry.Focus, it will cause a display error.

The solution is that Picker loses focus after the SelectedIndexChanged method runs, and then sets Entry.Focus to display it correctly.

Use Task to open a new thread to complete this operation.

Here is the backgroundcode:

public partial class MainPage : ContentPage
{   
    public MainPage()
    {
        InitializeComponent();
    }
    private async void picker_SelectedIndexChanged(object sender, EventArgs e)
    {
        var a = picker.IsFocused;
        string res = (sender as Picker).SelectedItem.ToString();
        Task.Run(()=> myTask(res));//Create and start the thread
    }

    private void myTask(string res)
    {
        Thread.Sleep(300);
        var a = picker.IsFocused;
        if (res == "Test1")
        {
            entry1.Focus();
        }
        if (res == "Test2")
        {
            entry2.Focus();
        }    
    }
}
  • Related