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