如何获得Android的软键盘高度? [英] How to get Android soft keyboard height?
问题描述
我知道我可以使用机器人: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.
- 我有与在屏幕的底部透明背景临时视图的地方。
- 我已经加入
的android:windowSoftInputMode =adjustResize
旗活动标记像@bill清单显示 -
现在主要的故事是
onGlobalLayout()
。在那里,我计算出的临时视图的Y轴
和高度根视图
- I have places on temporary view with transparent background at the bottom of the screen.
- I have added
android:windowSoftInputMode="adjustResize"
flag in activity tag in manifest like @bill suggests. Now main story is in
onGlobalLayout()
. There i calculate the difference between they axis
of temp view and height ofroot 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屋!