如何获得Android的软键盘高度? [英] How to get Android soft keyboard height?

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

问题描述

我知道我可以使用机器人:windowSoftInputMode =adjustPan做一些自动偏移但我真的想要得到的软件键盘高度,一些特殊用途

I know I can use "android:windowSoftInputMode="adjustPan" to do some automatically offset. But I really want to get software keyboard height for some special purpose.

我发现这里有一个类似的话题:<一href=\"http://stackoverflow.com/questions/13534365/getting-the-dimensions-of-the-soft-keyboard\">Getting软键盘的尺寸。但很明显,它不是一个通用的解决方案。

I got to find that there's a similar topic here: Getting the dimensions of the soft keyboard. But obviously, it's not an universal solution.

有没有一种通用的方式或内置的方法来获取软键盘高度? (或者如何才能得到当前光标所在位置和软件键盘顶部位置之间的偏移值?)

Is there a common way or built-in method to get soft keyboard height? (or how can I get the offset value between current cursor position and software keyboard top position?)

非常感谢

推荐答案

下面我的解决方案,这也是哈克,但解决问题。

Here my solution, it is also hacky but solve the problem.


  1. 我有与在屏幕的底部透明背景临时视图的地方。

  2. 我已经加入的android:windowSoftInputMode =adjustResize旗活动标记像@bill清单显示

  3. 现在主要的故事是 onGlobalLayout()。在那里,我计算出的临时视图的 Y轴和高度根视图

  1. I have places on temporary view with transparent background at the bottom of the screen.
  2. I have added android:windowSoftInputMode="adjustResize" flag in activity tag in manifest like @bill suggests.
  3. Now main story is in onGlobalLayout(). There i calculate the difference between the y axis of temp view and height of root view

final View view = findViewById(R.id.base);
view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

@Override
public void onGlobalLayout() {

    int rootViewHeight = view.getRootView().getHeight();
    View tv = findViewById(R.id.temp_view);
    int location[] = new int[2];
    tv.getLocationOnScreen(location);
    int height = (int) (location[1] + tv.getMeasuredHeight());
    deff = rootViewHeight - height;
    // deff is the height of soft keyboard
}
});


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

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