Android 軟鍵盤與自定義鍵盤 切換

記錄一下工作中遇到的問題

問題:、自定義鍵盤   完全是用layout的show  hidd  實現的。會與軟鍵盤存在問題,就是軟鍵盤彈出的時候,自定義的佈局也在顯示,從而導致   上面是自定義鍵盤   下面是軟鍵盤。

解決:想到監聽軟鍵盤的彈出  隱藏 從而控制自定義鍵盤

使用:buttonBeyondKeyboardLayout((ViewGroup) ((mActivity).getWindow().getDecorView().findViewById(android.R.id.content)));
private void buttonBeyondKeyboardLayout(final View root) {

        mListener = new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                final Rect rect = new Rect();
                root.getWindowVisibleDisplayFrame(rect);
                final int screenHeight = root.getRootView().getHeight();
                final int heightDifference = screenHeight - rect.bottom;
                //計算控件
                int[] location = new int[2];
                bottomLayout.getLocationOnScreen(location);
//                LogUtil.e("lishuai----計算得到控件在屏幕中的位置222-----:" + location[1]);
                // 事件爲彈出軟鍵盤時,軟件盤至少有100px吧 這裏100可以替換爲軟件盤的高度
                if (heightDifference > screenHeight / 3) {
//                    LogUtil.e("lishuai----出---顯示軟鍵盤");
                    //顯示軟鍵盤  判斷是否兩個鍵盤都在顯示
                    if (location[1] < screenHeight / 3) {
                        if (bottomLayout.getVisibility() == View.VISIBLE) {
                            //顯示軟鍵盤
                            if (!mCanKeyBoard) {
                                InputMethodUtil.hideSystemKeyboardByHideNotAlways(mActivity);
                                mCanKeyBoard = true;
                            } else {
                                keyboardlines.setVisibility(View.VISIBLE);
                                textlines.setVisibility(View.GONE);
                                imagelines.setVisibility(View.GONE);
                                reportlines.setVisibility(View.GONE);
                                bottomLayout.setVisibility(View.GONE);
                                mCanKeyBoard = false;
                            }

                        }
                    } else {
                        mCanKeyBoard = false;
                    }


                } else {
                    // 軟鍵盤隱藏進行還原
//                    LogUtil.e("lishuai----收");
                }
            }
        };
        mTreeObserver = root.getViewTreeObserver();
        mTreeObserver.addOnGlobalLayoutListener(mListener);
    }

 

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