Getting the dimensions of the soft keyboard

后端 未结 11 752
故里飘歌
故里飘歌 2020-11-27 14:56

Is there a way to know the size of the keyboard that is shown in the screen?

I am using Cocos2dx for programming, but I want to know the height of the keyboard shown

11条回答
  •  -上瘾入骨i
    2020-11-27 15:17

    if your activity is not fullscreen, using code below:

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

提交回复
热议问题