输入时自动弹出键盘

时间:2019-09-12 21:15:52

标签: xamarin.forms

我正在使用Xamain表单中的网格,我想在选择事件中调用我认为可以使这个单词生效的键盘。

我正在使用插件对话框工具包来显示数字条目,但我的问题是键盘仅在焦点对准时才显示在文本框中,如何强制键盘抬起以便用户不必单击进入领域。

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

“对话”工具包中的代码表明它试图将焦点放在输入字段上。

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

您可以看到哪个正在调用,但是我认为它的位置是错误的,因为它在弹出视图之前就已经存在。

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

1 个答案:

答案 0 :(得分:0)

我进行了一些测试,发现您应该在FocusEntry之后致电PopUp,以强制键盘自动启动。

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