1

在我的布局中有一个用于搜索的 EditText,并且在 EditText 下方有一个列表。现在我在 PopupWindow 中膨胀这个布局。现在我的问题是

1)如果我将 PopupWindow focusable 设置为 true ,那么除了 PopupWindow 布局之外的所有地方都变得不可点击。所以我不能通过点击外部或点击设备的后退按钮来关闭窗口

2)如果我设置 PopupWindow focusable false ,那么 Popupwindow 打开和关闭是完美的,但是在点击 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;
                }
            });
4

2 回答 2

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不能为空。在此处查看此答案

于 2013-10-17T16:32:37.480 回答
0

在代码中添加三行:

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

并且,popupWindow 的显示必须在最后被调用。

于 2015-09-23T03:30:21.950 回答