How to check visibility of software keyboard in Android?

前端 未结 30 4416
半阙折子戏
半阙折子戏 2020-11-21 04:43

I need to do a very simple thing - find out if the software keyboard is shown. Is this possible in Android?

30条回答
  •  你的背包
    2020-11-21 05:35

    Instead of assuming the difference coding I did something like this, as I dint had menu options in my application.

    final View root= findViewById(R.id.myrootview); 
    root.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
        public void onGlobalLayout() {
            int heightDiff = root.getRootView().getHeight() - root.getHeight();
    
            Rect rectgle= new Rect();
            Window window= getWindow();
            window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
            int contentViewTop=                     
              window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
            if(heightDiff <= contentViewTop){
                //Soft KeyBoard Hidden
            }else{
                //Soft KeyBoard Shown
            }
         }
    });
    

提交回复
热议问题