Android监听键盘显示和隐藏

问题概况:横板cocos2dx游戏,点击输入框弹出键盘时,界面要求跟随网上平易,不能挡住输入框。这种问题只出现在非全屏键盘到情况下。

方案1:mainActivity重写onconfigurationChanged,监听屏幕方向旋转,添加Android:configChanges="orientation|keyboard"。

缺点全屏下无效,如果设置为非全屏,<activityandroid:theme="@android:style/Theme.NoTitleBar.Fullscreen" />,去掉Fullscreen

这样会显示系统状态栏。否掉!

方案2:弹出键盘时势必引起layout布局的变化,监听布局的变化然后计算偏移,即可算出是否时显示或隐藏键盘。

//获取rootviewmRoot=this.getWindow().getDecorView();

全局键盘显示和隐藏不会触发

//添加布局变化监听root.getViewTreeObserver().addOnGlobalLayoutListener(newOnGlobalLayoutListener(){@OverridepublicvoidonGlobalLayout(){Rectrect=newRect();root.getWindowVisibleDisplayFrame(rect);introotInvisibleHeight=root.getRootView().getHeight()-rect.bottom;if(rootInvisibleHeight<=100){//通知c++做想做的事hideKeyBoardHandler();}else{showKeyBoardHandler();}}});

以上!另外对APP进行在线全方位的安全性、兼容性测试,我都会用这个:www.ineice.com。