Android

关注公众号 jb51net

关闭
首页 > 软件编程 > Android > Android监听软键盘显示状态

Android中监听软键盘显示状态实现代码

投稿:junjie

这篇文章主要介绍了Android中监听软键盘显示状态实现代码,本文直接给出核心实现代码,需要的朋友可以参考下
/**监听软键盘状态
   * @param activity
   * @param listener
   */
  public static void addOnSoftKeyBoardVisibleListener(Activity activity, final OnSoftKeyBoardVisibleListener listener) {
    final View decorView = activity.getWindow().getDecorView();
    decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {
        Rect rect = new Rect();
        decorView.getWindowVisibleDisplayFrame(rect);
        int displayHight = rect.bottom - rect.top;
        int hight = decorView.getHeight();
        boolean visible = (double) displayHight / hight < 0.8;
         
        Log.d(TAG, "DecorView display hight = " + displayHight);
        Log.d(TAG, "DecorView hight = " + hight);
        Log.d(TAG, "softkeyboard visible = " + visible);
 
        if(visible != sLastVisiable){
          listener.onSoftKeyBoardVisible(visible);
        }
        sLastVisiable = visible;
      }
    });
  }

您可能感兴趣的文章:
阅读全文