Xamarin表格检查键盘是否打开 [英] Xamarin forms check if keyboard is open or not

查看:113
本文介绍了Xamarin表格检查键盘是否打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以检查Xamarin Forms中的键盘是否打开?键盘打开或关闭时是否会触发任何事件?如果是这样,我在哪里可以找到示例?

Is there any way to check if keyboard is open or not in Xamarin Forms? Are there any events getting fired when the keyboard opens or closes? If so, where can I find an example of it?

推荐答案

我不认为有Xamarin.Forms方式可以做到.无论如何,对于不同的平台(至少是Android和iOS),有一种方法可以实现您想要的.

I don't believe that there's a Xamarin.Forms way of doing it. Anyway, for the different platforms (at least Android and iOS) there is a way to achieve what you want.

在android下,有一个InputMethodManager类.您可以从自己的活动中获取它

Under android there is InputMethodManager class. You can obtain it from your activity

var inputMethodManager = (InputMethodManager)this.GetSystemService(Context.InputMethodService);

现在您可以检查键盘是否显示为

Now you can check if the keyboard is shown with

var keyboardIsShown = inputMethodManager.IsAcceptingText;

根据有关CodeProject的这篇文章,您可以使用从IOnGlobalLayoutListener派生的类来侦听全局布局事件.触发此事件后,您可以使用上面的代码检查布局是否由于键盘弹出而发生了更改.

According to this article on CodeProject you can use a class derived from IOnGlobalLayoutListener to listen to global layout events. When this event has fired, you can use the code above to check, if the layout has been changed due to the keyboard popping up.

在iOS下,您可以使用UIKeyboard类,该类允许您观察DidShowNotification(请参见此处).

Under iOS you may use UIKeyboard class which allows you to observe the DidShowNotification (see here).

notification = UIKeyboard.Notifications.ObserveDidShow ((sender, args) => {
    Debug.WriteLine("Keyboard is shown.");
    // whatever
});

类似地,您可以观察到DidHideNotification(以及其他一些内容-请参见此处).

similarly you can observe DidHideNotification (and some others - see here).

要在Xamarin.Forms中实现键盘通知,最简单的方法是实现由DependencyService解析的平台依赖项.为此,您首先必须为平台服务引入一个接口.

To implement the keyboard-notification in your Xamarin.Forms the easiest way will be to implement platform dependencies which are resolved with the DependencyService. To do this, you'll first have to introduce an interface for the platform service.

public interface IKeyboardService
{
  event EventHandler KeyboardIsShown;
  event EventHandler KeyboardIsHidden;
}

在特定于平台的项目中,您将必须以特定于平台的方式来实现功能.请参阅以下代码部分以了解iOS实现

In your platform specific projects you'll have to implement the functionality in a platform specific way. See the following code section for iOS implementation

[assembly: Xamarin.Forms.Dependency(typeof(Your.iOS.Namespace.KeyboardService))]

namespace Your.iOS.Namespace
{
  public class KeyboardService : IKeyboardService
  {
    public event EventHandler KeyboardIsShown;
    public event EventHandler KeyboardIsHidden;

    public KeyboardService()
    {
      SubscribeEvents();
    }

    private void SubscribeEvents()
    {
      UIKeyboard.Notifications.ObserveDidShow(OnKeyboardDidShow);
      UIKeyboard.Notifications.ObserveDidHode(OnKeyboardDidHide);
    }

    private void OnKeyboardDidShow(object sender, EventArgs e)
    {
      KeyboardIsShown?.Invoke(this, EventArgs.Empty);
    }

    private void OnKeyboardDidHide(object sender, EventArgs e)
    {
      KeyboardIsHidden?.Invoke(this, EventArgs.Empty);
    }
  }
}

Xamarin.Forms.Dependency使该类对DependencyService可见.请参阅以下用于Android实现的代码

The Xamarin.Forms.Dependency makes the class visible to the DependencyService. See the following code for Android implementation

[assembly: Xamarin.Forms.Dependency(typeof(Your.Android.Namespace.KeyboardService))]

namespace Your.Android.Namespace
{
  public class KeyboardService : IKeyboardService
  {
    public event EventHandler KeyboardIsShown;
    public event EventHandler KeyboardIsHidden;

    private InputMethodManager inputMethodManager;

    private bool wasShown = false;

    public KeyboardService()
    {
      GetInputMethodManager();
      SubscribeEvents();
    }

    public void OnGlobalLayout(object sender, EventArgs args)
    {
      GetInputMethodManager();
      if(!wasShown && IsCurrentlyShown())
      {
        KeyboardIsShown?.Invoke(this, EventArgs.Empty);
        wasShown = true;
      }
      else if(wasShown && !IsCurrentlyShown())
      {
        KeyboardIsHidden?.Invoke(this, EventArgs.Empty);
        wasShown = false;
      }
    }

    private bool IsCurrentlyShown()
    {
      return inputMethodManager.IsAcceptingText;
    }

    private void GetInputMethodManager()
    {
      if (inputMethodManager == null || inputMethodManager.Handle == IntPtr.Zero)
      {
        inputMethodManager = (InputMethodManager)this.GetSystemService(Context.InputMethodService);
      }
    }

    private void SubscribeEvents()
    {
      ((Activity)Xamarin.Forms.Forms.Context).Window.DecorView.ViewTreeObserver.GlobalLayout += this.OnGlobalLayout;
    }
  }
}

现在,在您的Xamarin.Forms应用程序中,您可以使用获取正确实现IKeyboardService的实例.

In your Xamarin.Forms app you can now obtain an instance of the correct implementation of IKeyboardService with

var keyboardService = Xamarin.Forms.DependencyService.Get<IKeyboardService>();  

这篇关于Xamarin表格检查键盘是否打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆