Android中有什么方法可以获取设备虚拟键盘的高度 [英] Is there any way in Android to get the height of virtual keyboard of device

查看:16
本文介绍了Android中有什么方法可以获取设备虚拟键盘的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android 中是否有任何方法可以在运行时获取设备上显示的虚拟键盘的高度?其实我想在键盘上方显示一个文本框.

Is there any way in Android to get the height of the virtual keyboard displayed on the device in run time? Actually I want to show a text box above the keyboard.

推荐答案

可以,在 Viewtree Observer 和全局布局监听器的帮助下,试试下面提到的步骤

Yes you can, with the help of Viewtree Observer and global layout listener, just try below mentioned steps

  1. 获取布局的根视图
  2. 获取此根的 Viewtree 观察者,并在此之上添加一个全局布局侦听器.

现在,每当显示软键盘时,android 都会重新调整您的屏幕大小,并且您会收到听者的呼叫.这就是您现在需要做的唯一一件事就是计算您的根视图在重新调整大小后的高度与原始大小之间的差异.如果差值大于 150,则认为这是键盘已充气.

now whenever soft keyboard is displayed android will re-size your screen and you will receive call on your listener. That's it only thing you now need to do is calculate difference between height which your root view has after re-size and original size. If difference is more then 150 consider this as a keyboard has been inflated.

下面是示例代码

root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
     public void onGlobalLayout(){
           int heightDiff = root.getRootView().getHeight()- root.getHeight();
           // IF height diff is more then 150, consider keyboard as visible.  
        }
  });

问候,技术拳

这篇关于Android中有什么方法可以获取设备虚拟键盘的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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