dotnetdevcsharp
dotnetdevcsharp

Reputation: 3980

Make Keyboard popup automatically on entry

I am using a grid in Xamain forms and I want on the selection event to call the keyboard I thought doing this word wok.

I am using the plugin dialogue kit to display a numeric entry but my quesiton is the keyboard is only displaying on the text box when it has focus how Do I force the keyboard to come up so the user does not have to click into the field.

 new Entry()
 {
                Keyboard = Keyboard.Numeric
 };

 var resultQty = await Plugin.DialogKit.CrossDiaglogKit.Current.GetInputTextAsync("Test", $"Please goto Bin {item.BinName} , and enter the visible stock of the item." + item.Name, null, Keyboard.Numeric);

The code from Dialog kit shows that it attempts to place a focus on the entry field.

<ContentView.Content>
    <StackLayout Padding="10" BackgroundColor="White" VerticalOptions="CenterAndExpand" Margin="25">
        <Label FontAttributes="Bold" FontSize="Large" Text="{Binding Title}"/>
        <Label FontSize="Large" Text="{Binding Message}"/>
        <Entry x:Name="txtInput" Keyboard="{Binding Keyboard}"/>
        <StackLayout Margin="10" Orientation="Horizontal">
            <Button Text="{Binding OK}" Clicked="Confirm_Clicked" HorizontalOptions="FillAndExpand"/>
            <Button Text="{Binding Cancel}" Clicked="Cancel_Clicked" HorizontalOptions="FillAndExpand"/>
        </StackLayout>
    </StackLayout>
</ContentView.Content>

You will see here that the dialog kit calls the above few as such

public Task<string> GetInputTextAsync(string title, string message,string currentText = null, Keyboard keyboard = null)
{           
        if (keyboard == null) keyboard = Keyboard.Default;
        var cts = new TaskCompletionSource<string>();
        var _dialogView = new Plugin.DialogKit.Views.InputView(title, message,currentText,keyboard);
        _dialogView.FocusEntry();
        _dialogView.Picked += (s, o) => { cts.SetResult(o); PopupNavigation.PopAsync(); };
        PopupNavigation.PushAsync(new PopupPage { Content = _dialogView });
        return cts.Task;
  }

Which as you can see is calling the but i think the placement of this is wrong as its before its pops onto the view.

public void FocusEntry() { txtInput.Focus(); }

Upvotes: 2

Views: 610

Answers (1)

nevermore
nevermore

Reputation: 15806

I did some test and found you should Call the FocusEntry after PopUp to force the keyboard to come up automatically.

private async void Button_Clicked(object sender, EventArgs e)
{
    var resultQty = await GetInputTextAsync("Test", $"Please goto Bin, the visible stock of the item.", null, Keyboard.Numeric);
}

public async Task<string> GetInputTextAsync(string title, string message, string currentText = null, Keyboard keyboard = null)
{
    if (keyboard == null) keyboard = Keyboard.Default;
    var cts = new TaskCompletionSource<string>();
    var _dialogView = new Plugin.DialogKit.Views.InputView(title, message, currentText, keyboard);
    _dialogView.Picked += (s, o) => { cts.SetResult(o); PopupNavigation.PopAsync(); };
    await PopupNavigation.PushAsync(new PopupPage { Content = _dialogView });

    //Call the FocusEntry after PopUp
    _dialogView.FocusEntry();

    return await cts.Task;
}

Upvotes: 1

Related Questions