简体   繁体   English

如何检测android键盘何时出现/消失? (5.02)

[英]How can I detect when android keyboard appears/disappears? (5.02)

I updated my phone and the code I had which worked before does not work in 5.02 (new version I just updated phone to). 我更新了手机,以前使用过的代码在5.02(我将手机更新到的新版本)中不起作用。 Is there a way that will work with all versions? 有没有一种方法可以适用于所有版本? Here is my code that does not work anymore (it says it is visible when it is not): 这是我的代码不再起作用(它说不可见时可见):

final View activityRootView = findViewById(R.id.mainRelativeLayout);
        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout()
            {
                int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();

                if (heightDiff > 100)
                {
                    System.out.println("test:    keyboard visible");

                    RelativeLayout.LayoutParams lp =(RelativeLayout.LayoutParams)bottomSelectionLayout.getLayoutParams();
                    lp.height = 0;
                    bottomSelectionLayout.setLayoutParams(lp);

                    RelativeLayout.LayoutParams lp2 =(RelativeLayout.LayoutParams)bottomSeperatorLine.getLayoutParams();
                    lp2.height = 0;
                    bottomSeperatorLine.setLayoutParams(lp2);

                    bottomSelectionLayout.setVisibility(View.INVISIBLE);
                    bottomSeperatorLine.setVisibility(View.INVISIBLE);
                }

                else
                {
                    System.out.println("test:    keyboard invisible");

                    RelativeLayout.LayoutParams lp =(RelativeLayout.LayoutParams)bottomSelectionLayout.getLayoutParams();
                    lp.height = (int) bottomSelectionLayoutStartingHeight;
                    bottomSelectionLayout.setLayoutParams(lp);

                    RelativeLayout.LayoutParams lp2 =(RelativeLayout.LayoutParams)bottomSeperatorLine.getLayoutParams();
                    lp2.height = (int)bottomSeperatorLineStartingHeight;
                    bottomSeperatorLine.setLayoutParams(lp2);

                    bottomSelectionLayout.setVisibility(View.VISIBLE);
                    bottomSeperatorLine.setVisibility(View.VISIBLE);
                }
            }
        });

Can you add the following line to your layout XML? 您可以将以下行添加到布局XML吗? This will help support android:windowSoftInputMode="adjustResize" that you have added to your AndroidManifest.xml in versions 21 and greater 这将有助于支持您已添加到版本21及更高版本的AndroidManifest.xml中的android:windowSoftInputMode="adjustResize"

android:fitsSystemWindows="true"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 键盘出现时BottomNavigationBar消失 - BottomNavigationBar disappears when keyboard appears 单击搜索输入时,Android 键盘出现和消失 - Android keyboard appears and disappears when clicking search input 键盘出现时如何正确调整布局大小? - How can I correctly resize the layout when the keyboard appears? 出现键盘时如何处理Android中的网站 - How to handle websites in Android when keyboard appears Android:屏幕键盘出现和消失时,是否有自动调用的监听器? - Android: When the on-screen keyboard appears and disappears, are there any listeners that are automatically called? android Edit Text键盘出现,并在可扩展列表中突然消失 - android Edit Text keyboard appears and suddenly disappears in expandable list 在软键盘出现时出现状态栏但在沉浸模式下软键盘消失时不会隐藏 - Status Bar appears when soft keyboard appears but not hidden back when soft keyboard disappears while in Immersive Mode 当软键盘出现或消失时,Recyclerview 向下滚动 - Recyclerview scrolls down when soft keyboard appears or disappears 如何检测何时隐藏Android软件键盘? - How to detect when Android software keyboard is hidden? 键盘出现时如何使android视图可滚动? - how to make an android view scrollable when the keyboard appears?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM