首页 > 系统 > Android > 正文

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

2020-04-11 11:35:10
字体:
来源:转载
供稿:网友
/**监听软键盘状态   * @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;      }    });  }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表