keyboard - 如何在 Xamarin Forms 中按下按钮时关闭键盘

标签 keyboard xamarin.forms

经过大量搜索,我找到了一种在 Xamarin Forms for iOS 中按下按钮时隐藏键盘的方法。所以分享如下。

如果有人可以改进它,或者分享 Android 端的解决方案,那就太好了。

最佳答案

我发现这很有用:

https://forums.xamarin.com/discussion/comment/172077#Comment_172077

界面:

public interface IKeyboardHelper
{
    void HideKeyboard();
}

iOS:

public class iOSKeyboardHelper : IKeyboardHelper
{
    public void HideKeyboard()
    {
        UIApplication.SharedApplication.KeyWindow.EndEditing(true);
    }
}

机器人:

public class DroidKeyboardHelper : IKeyboardHelper
{
    public void HideKeyboard()
    {
        var context = Forms.Context;
        var inputMethodManager = context.GetSystemService(Context.InputMethodService) as InputMethodManager;
        if (inputMethodManager != null && context is Activity)
        {
            var activity = context as Activity;
            var token = activity.CurrentFocus?.WindowToken;
            inputMethodManager.HideSoftInputFromWindow(token, HideSoftInputFlags.None);

            activity.Window.DecorView.ClearFocus();
        }
    }
}

Xamarin 表单中的用法:

DependencyService.Get<IKeyboardHelper>().HideKeyboard();

关于keyboard - 如何在 Xamarin Forms 中按下按钮时关闭键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30437362/

相关文章:

android - 在 Android 中隐藏键盘

android - 表情符号在键盘按键标签和 textView 上看起来不同

actionscript-3 - 在as3中禁用重复键盘按下事件

ios - 对象引用未设置到对象的实例 - Select 语句 Sqlite Xamarin iOS

jquery - 使用Jquery模拟键盘按键

c# - .NET : How to get time for keypress as precisely as possible?

datepicker - Xamarin Forms - 自定义日期选择器的 FontFamily、FontSize

xamarin - 如何在 Xamarin.Forms 中存储应用设置

xamarin.forms - 如何在 Xamarin.Forms 的每个平台中访问公共(public)下载文件夹

c# - 重新启动应用程序时来自 Xamarin 消息中心的多条消息