PopupWindow不会关闭后退键

时间:2013-06-26 12:04:04

标签: android

在我的版面中有一个用于搜索的EditText,EditText下面有一个列表。现在我在PopupWindow中为这个布局充气。 现在我的问题是

1)如果我将PopupWindow设置为focusable为true,则弹出窗口布局以外的所有位置都将变为不可点击。因此,我无法通过单击外部或单击设备的后退按钮来关闭窗口

2)如果我将PopupWindow设置为focusable false,那么弹出窗口打开和关闭是完美的,但是软键盘在EditText上无法显示

LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // Inflate the view from a predefined XML layout View layout = inflater.inflate(R.layout.popup_layout, null);
mPopUpWindow= new PopupWindow(layout,0, mScreenHeight - 100, false);
mPopUpWindow.setBackgroundDrawable(null);
mPopUpWindow.setOutsideTouchable(true);
mPopUpWindow.setTouchInterceptor(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    // TODO Auto-generated method stub
                    if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                        mPopUpWindow.dismiss();

                    }
                    return false;
                }
            });

final InputMethodManager inputMgr = (InputMethodManager) MainApplication.getContext().getSystemService(
                    Context.INPUT_METHOD_SERVICE);
inputMgr.showSoftInput(mEditText, InputMethodManager.SHOW_IMPLICIT);
mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_SEARCH) {

inputMgr.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(),
                                InputMethodManager.HIDE_NOT_ALWAYS);

                        return true;
                    }
                    return false;
                }
            });

2 个答案:

答案 0 :(得分:0)

只需使用此

即可
View popupView= getLayoutInflater().inflate(R.layout.popupmenu,null);
                PopupWindow popup = new PopupWindow(popupView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT,true);
                popup.setBackgroundDrawable(new BitmapDrawable());
                popup.setOutsideTouchable(true);
                popup.showAsDropDown(view);
                popup.update();

重点是PopUpWindow不能为空。请参阅此回答here

答案 1 :(得分:0)

在代码中添加三行:

popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.showAsDropDown(ivmainmenu);

而且,最后必须调用popupWindow的节目。