[英]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.