1. 程式人生 > >Android PopupWindow與軟鍵盤的遮擋問題

Android PopupWindow與軟鍵盤的遮擋問題

PopupWindow含有輸入框時,點選輸入框,軟鍵盤可能會擋住PopupWindow,而我們希望的是軟鍵盤能夠把PopupWindow給頂上去。

設定 popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); 

InputMethodManager m=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); (這個方法可以實現輸入法在視窗上切換顯示,如果輸入法在視窗上已經顯示,則隱藏,如果隱藏,則顯示輸入法到視窗上)

InputMethodManager inputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            if (inputMethodManager.isActive()) {
                inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
            }

            popWindow = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
            popWindow.setOutsideTouchable(true);
            popWindow.setFocusable(true);
            popWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
            popWindow.setAnimationStyle(R.style.anim_edit_text_popup);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    popWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
                }
            }, 50);