Android: how can i tell if the soft keyboard is showing or not?

前端 未结 5 1842
旧巷少年郎
旧巷少年郎 2020-11-29 08:12

Heres the dilemma: I am showing a screen with 3 input fields and 2 buttons inside of a tab(there are 3 tabs total, and they are on the bottom of the screen). the 2 buttons

5条回答
  •  一向
    一向 (楼主)
    2020-11-29 08:48

    Working solution for me -

    ViewTreeObserver treeObserver = getViewTreeObserver();
            if (treeObserver != null)
                treeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        int pHeight = getResources().getDisplayMetrics().heightPixels;
    
                    Rect visRect = new Rect();
                    viewGroup.getWindowVisibleDisplayFrame(visRect);
    
                    boolean keyboardVisible;
                    int keyboardHeight= pHeight-currentHeight;
                    if (keyboardHeight > (MIN_KEYBOARD_HEIGHT*pHeight))
                    {
                        TGVLog.d(debugTag, "keyboardVisible-- "+true);
                        keyboardVisible = true;
                    }
                    else
                    {
                        TGVLog.d(debugTag, "keyboardVisible-- "+false);
                        keyboardVisible = false;
                    }
                    }
                });
    

提交回复
热议问题