首页 > 解决方案 > 输入时自动弹出键盘

问题描述

我在 Xamain 表单中使用网格,我想在选择事件上调用我认为做这个词 wok 的键盘。

我正在使用插件对话工具包来显示一个数字条目,但我的问题是键盘仅在具有焦点时才显示在文本框上我如何强制键盘出现以便用户不必单击该字段。

 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);

Dialog kit 中的代码显示它试图将焦点放在输入字段上。

<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>

您将在此处看到对话工具包调用上述几个

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;
  }

正如您所看到的那样,它正在调用,但我认为它的位置是错误的,因为它在它弹出到视图之前。

公共无效 FocusEntry() { txtInput.Focus(); }

​</p>

标签: xamarin.forms

解决方案


我做了一些测试,发现你应该调用FocusEntryafterPopUp来强制键盘自动出现。

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;
}

推荐阅读