0
我更新了我的手機和我以前工作過的代碼在5.02(新版本,我只是更新手機)無法正常工作。有沒有一種方法可以與所有版本兼容?這裏是我的代碼,不再工作(它說它不可見時):如何檢測Android鍵盤出現/消失的時間? (5.02)
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);
}
}
});
問題是鍵盤未正確檢測到。當它不在時它被檢測爲可見。無論我嘗試將你的代碼添加到我的清單並且它不能解決問題:( – JessThePest
好吧,這裏有更多的信息可以幫助你。當鍵盤可見時,heightDiff是146.當鍵盤不可見時,heightDiff是670 。 – JessThePest