【问题标题】:How to hide keyboard on Editor when needed需要时如何在编辑器上隐藏键盘
【发布时间】:2018-08-29 22:13:03
【问题描述】:

我使用Xamarin进行开发。

当我点击Editor 时,控制键盘出现。我希望在用户写消息并单击输入时让键盘消失,甚至在用户未决定写任何内容时以某种方式隐藏键盘。

有办法吗?

【问题讨论】:

    标签: xamarin.forms


    【解决方案1】:

    你也可以使用

    editor.UnFocus(); 
    

    【讨论】:

      【解决方案2】:

      你试试这个。首先在你的共享项目中创建接口

      public interface IKeyboardHelper
      {
          void HideKeyboard();
      }
      

      通过添加新的 iOSKeyboardHelper.cs 文件在 iOS 项目中实现了这一点

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

      现在通过添加新文件 DroidKeyboardHelper.cs 在您的 android 项目中实现相同的功能

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

      像这样在共享项目中调用你的方法,无论你需要它。

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

      访问thisthis了解更多信息。

      【讨论】:

        猜你喜欢
        • 2012-08-11
        • 1970-01-01
        • 2022-10-24
        • 1970-01-01
        • 1970-01-01
        • 2019-07-12
        • 2019-06-08
        • 2014-03-19
        相关资源
        最近更新 更多