获得软键盘的尺寸

Rud*_*_TM 25 keyboard android cocos2d-x

有没有办法知道屏幕上显示的键盘大小?

我正在使用Cocos2dx进行编程,但我想知道Android部分或Cocos部分屏幕中显示的键盘高度,这没关系.

我知道Keyboard有一个getHeight()方法,但我不想创建新的键盘,我想使用默认键盘.

Rud*_*_TM 35

我们这样做了

myLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

                @Override
                public void onGlobalLayout() {

                    Rect r = new Rect();
                    parent.getWindowVisibleDisplayFrame(r);

                    int screenHeight = parent.getRootView().getHeight();
                    int heightDifference = screenHeight - (r.bottom - r.top);
                    Log.d("Keyboard Size", "Size: " + heightDifference);

                }
            });
Run Code Online (Sandbox Code Playgroud)

我们只用键盘调整视图大小,所以我们可以使用它.

  • 对我来说(Nexus 7与Android 5.0)它确实考虑了系统按钮. (4认同)
  • 如果屏幕上显示的不仅仅是键盘,即系统按钮(背面,家庭等),它将无法工作 (2认同)

FDI*_*DIM 21

Rect r = new Rect();
View rootview = this.getWindow().getDecorView(); // this = activity
rootview.getWindowVisibleDisplayFrame(r);
Run Code Online (Sandbox Code Playgroud)

结果是您的应用程序在屏幕上使用的空间量(即使调整活动大小时也能正常工作).显然键盘将使用剩余的屏幕空间(如果可见)

在这里找到id:https://github.com/freshplanet/ANE-KeyboardSize/blob/master/android/src/com/freshplanet/ane/KeyboardSize/getKeyboardY.java


小智 9

如果您的活动不是全屏,请使用以下代码:

content.getViewTreeObserver().addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {

                @Override
                public void onGlobalLayout() {
                    // TODO Auto-generated method stub
                    if (keyBoardHeight <= 100) {
                        Rect r = new Rect();
                        content.getWindowVisibleDisplayFrame(r);

                        int screenHeight = content.getRootView()
                                .getHeight();
                        int heightDifference = screenHeight
                                - (r.bottom - r.top);
                        int resourceId = getResources()
                                .getIdentifier("status_bar_height",
                                        "dimen", "android");
                        if (resourceId > 0) {
                            heightDifference -= getResources()
                                    .getDimensionPixelSize(resourceId);
                        }
                        if (heightDifference > 100) {
                            keyBoardHeight = heightDifference;
                        }

                        Log.d("Keyboard Size", "Size: " + heightDifference);
                    }
                    // boolean visible = heightDiff > screenHeight / 3;
                }
            });
Run Code Online (Sandbox Code Playgroud)


小智 5

如果要在活动大小不变(adjustPan)时计算虚拟键盘高度,则可以使用此示例:

https://github.com/siebeprojects/samples-keyboardheight

它使用隐藏窗口来计算窗口和活动的根视图之间的高度差.