android 监听软键盘在页面的展开和隐藏
生活随笔
收集整理的這篇文章主要介紹了
android 监听软键盘在页面的展开和隐藏
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
獲取軟鍵盤狀態思路:
相關代碼塊:
viewTree的監聽
// viewTree的監聽 private ViewTreeObserver.OnGlobalLayoutListener keyboardLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {@Overridepublic void onGlobalLayout() {// 顯示的根視圖 Rect r = new Rect();rootLayout.getWindowVisibleDisplayFrame(r);// 導航欄對象Point navigationBarHeight = SystemUtils.getNavigationBarSize(InformationBaseActivity.this);// 狀態欄高度int statusBarHeight = SystemUtils.getStatusBarHeight(InformationBaseActivity.this);Log.e(TAG, "navigationBarHeight " + navigationBarHeight.x + " " + navigationBarHeight.y + statusBarHeight);// (r.bottom - r.top) 當前頁面根視圖顯示的高度// heightDiff 當前頁面原本根布局高度與顯示視圖的高度差int heightDiff = rootLayout.getRootView().getHeight() - (r.bottom - r.top);// 高度差判斷是否彈出軟鍵盤if (heightDiff > (100 + navigationBarHeight.y + statusBarHeight)) { // if more than 100 pixels, its probably a keyboard...mIsShowSoft = true;onShowKeyboard(heightDiff);} else {if (mIsShowSoft) {onHideKeyboard();mIsShowSoft = false;}}}};根布局對viewTree的實時監聽
protected void attachKeyboardListeners(int rootViewId) {if (keyboardListenersAttached) {return;}rootLayout = findViewById(rootViewId);rootLayout.getViewTreeObserver().addOnGlobalLayoutListener(keyboardLayoutListener);keyboardListenersAttached = true;}頁面銷毀時移除監聽
@Overridepublic void onDestroy() {super.onDestroy();if (keyboardListenersAttached) {rootLayout.getViewTreeObserver().removeGlobalOnLayoutListener(keyboardLayoutListener);}}如何使用:
這些軟鍵盤的監聽操作可部署到基類中,需要用時可調用基類中的attachKeyboardListeners(),onShowKeyboard()和onHideKeyboard()方法進行相對應的操作。
項目下載地址
總結
以上是生活随笔為你收集整理的android 监听软键盘在页面的展开和隐藏的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: word选中所有图形
- 下一篇: 使用Visio 2003画UML类图之使