Android監聽鍵盤顯示和隱藏


Android監聽鍵盤顯示和隱藏

 

問題概況:橫板cocos2dx遊戲,點擊輸入框彈出鍵盤時,界面要求跟隨網上平易,不能擋住輸入框。這種問題只出現在非全屏鍵盤到情況下。

 

方案1:mainActivity重寫onconfigurationChanged,監聽屏幕方向旋轉,添加Android:configChanges="orientation|keyboard"

 

缺點全屏下無效,如果設置爲非全屏,<activityandroid:theme="@android:style/Theme.NoTitleBar.Fullscreen" />,去掉Fullscreen

 

這樣會顯示系統狀態欄。否掉!

 

方案2:彈出鍵盤時勢必引起layout佈局的變化,監聽佈局的變化然後計算偏移,即可算出是否時顯示或隱藏鍵盤。

//獲取rootview
mRoot =this.getWindow().getDecorView();

 

全局鍵盤顯示和隱藏不會觸發

//添加布局變化監聽
root.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    Rect rect = new Rect();
                   root.getWindowVisibleDisplayFrame(rect);
                    int rootInvisibleHeight =root.getRootView().getHeight() - rect.bottom;
                    if (rootInvisibleHeight<= 100) {
                        //通知c++做想做的事
                        hideKeyBoardHandler();
                    } else {
                        showKeyBoardHandler();
                    }
                }
            });

 

以上!另外對APP進行在線全方位的安全性、兼容性測試,我都會用這個:www.ineice.com


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章