检测软键盘何时显示/隐藏

时间:2013-06-11 01:03:25

标签: android keyboard

我正在尝试检测键盘何时显示为隐藏。我找到了这段代码,但它对我不起作用。

    final View activityRootView = findViewById(R.id.LL_main);
    activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Rect r = new Rect();
            //r will be populated with the coordinates of your view that area still visible.
            activityRootView.getWindowVisibleDisplayFrame(r);

            int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);

            Log.d("login" ,

                            String.valueOf(activityRootView.getRootView().getHeight()) +
                            " - " +
                            String.valueOf(r.bottom - r.top) +
                            " = " +
                            String.valueOf(heightDiff));

        }
    });

我的'LL_main'是一个LinearLayout,它是主要的布局

的Manifest.xml

android:windowSoftInputMode="adjustPan|adjustResize"

即时使用此代码:https://stackoverflow.com/a/9108219/1959076

0 个答案:

没有答案