我怎样才能获得软键盘高度android上? [英] how can i get soft keyboard height on android?

查看:178
本文介绍了我怎样才能获得软键盘高度android上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发使用libgdx这几天的Andr​​oid项目。在此期间发生的问题。当软键盘的出现,有些意见将被覆盖,所以我希望得到的高度来解决这个bug。

i have been developing an android project using libgdx for these days . A question occurred during the period. when soft keyboard' appear , some views will be covered,so i want to get the height for solving this bug.

我知道有可以使用Android API来开发项目时设置为解决这个问题,软输入模式,确实libgdx提供什么办法?

i know there is a soft input mode can be set to solve this issue when using android api to develop project ,does libgdx provide any way ?

推荐答案

当然可以,用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.  
        }
  });

这篇关于我怎样才能获得软键盘高度android上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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