如何关闭Xamarin Forms中的键盘

时间:2018-10-02 20:50:05

标签: xamarin.forms xamarin.android

我正在寻找一种在保持对“输入”字段的关注的情况下关闭键盘的方法,以便可以使用外部键盘而不会放弃所有屏幕空间。

我似乎无法在https://www.ibm.com/support/knowledgecenter/ean/SS6NHC/com.ibm.swg.im.dashdb.sql.ref.doc/doc/r_create_ext_table.html中实现代码,而是在Entry字段的OnFocused中调用它。

Android.App.Application.Context as Activity; 

返回null如果没有“作为活动,它工作正常,但我无法运行

.CurrentFocus?.WindowToken;

在上下文中。

这就是我的代码。

[assembly: Dependency (typeof (KeyboardInteractions))] namespace namespace.Droid
{
    public class KeyboardInteractions : IKeyboardInteractions
    {
        public void HideKeyboard()
        {
        var activity = Android.App.Application.Context as Activity;
        var token = activity?.CurrentFocus?.WindowToken;
        var i = (InputMethodManager)Android.App.Application.Context.GetSystemService(Context.InputMethodService);
        if (token != null) {
            i.HideSoftInputFromWindow(token, 0);
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

使用CurrentActivityPlugin

安装了插件后,您可以像这样使用它:

var activity = CrossCurrentActivity.Current?.Activity;

如有查询,请随时还原。