有没有办法在Android中获得设备的虚拟键盘的高度 [英] Is there any way in android to get the height of virtual keyboard of device

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

问题描述

有没有办法在Android中获得的Andr​​oid设备在运行时的虚拟键盘的高度。其实我想告诉文本框上方的键盘。

is there any way in android to get the height of virtual keyboard of android device in run time. Actually I want to show textbox above keyboard.

推荐答案

当然可以,用Viewtree观测和全球布局监听器的帮助下,只要尝试下述步骤

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.

下面是一个简单的code

Below is a sample code

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

问候, Techfist

Regards, Techfist

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

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