Android 监听键盘是否弹出

2019-08-15  本文已影响0人  Gao秋
private void listenKeyboardVisible() {

finl View contentView = findViewById(android.R.id.content);
    final View activityRoot = getWindow().getDecorView();
    if (activityRoot ==null) {
        return;
    }

contentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
private final Rectr =new Rect();
private final int visibleThreshold = Math.round(ConvertUtils.dp2px(100));

        @Override
        public void onGlobalLayout() {
            activityRoot.getWindowVisibleDisplayFrame(r);
            int heightDiff =activityRoot.getRootView().getHeight() -r.height();
            //键盘是否弹出
            boolean isOpen = heightDiff >visibleThreshold;

        }

});

}
上一篇 下一篇

猜你喜欢

热点阅读