Android弹出窗口不消失

3
我有以下代码,它创建一个包含EditText的PopupWindow:
lbs.setOnTouchListener(new OnTouchListener(){

        @Override
        public boolean onTouch(View v, MotionEvent arg1) {
            int pWidth = 100;
            int pHeight = 80;
            int vHeight = mView.getHeight();
            int[] location = new int[2];
            v.getLocationOnScreen(location);
            final View view = inflater.inflate(R.layout.list_popup, null, false);
            final PopupWindow pw = new PopupWindow(view, pWidth, pHeight, false);
            pw.setTouchable(true);
            //pw.setFocusable(true);
            pw.setOutsideTouchable(true);
            pw.setBackgroundDrawable(new BitmapDrawable());
            pw.setContentView(view);
            pw.showAtLocation(v, Gravity.NO_GRAVITY, location[0]-(pWidth/4), location[1]+vHeight);
            //final LinearLayout layout = (LinearLayout)view.findViewById(R.id.PopupLayout);

            final EditText input = (EditText)view.findViewById(R.id.Input);
            input.setOnFocusChangeListener(new View.OnFocusChangeListener() {

                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    Log.i("Focus", "Focus Changed");
                    /*
                    if (hasFocus) {
                        InputMethodManager inputMgr = (InputMethodManager)myContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                        inputMgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
                        inputMgr.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
                    }
                    */

                }
            });
            input.setText(lbs.getText().toString());
            input.requestFocus();
            pw.setOnDismissListener(new OnDismissListener(){

                @Override
                public void onDismiss() {
                    parentActivity.changeWeight(getId, Double.parseDouble(input.getText().toString()));
                    Log.i("View Visibility", "" + view.getVisibility());
                }

            });

            pw.setTouchInterceptor(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                        Log.i("Background", "Back Touched");
                        pw.dismiss();               
                        return true;
                    }
                    return false;
                }
            });

            return true;
        }   
    });

PopupWindows具有setBackGroundDrawable功能,因此当用户点击窗口外部时,应自动关闭该框。我知道该框已经关闭,因为当我在框外单击时,我的OnDismiss方法运行,但实际窗口直到我在框外部两次点击后才消失,因此onDismiss运行了两次,这是不可接受的。我在这里错过了什么?
编辑:我稍微更新了一下代码。如果我不在popupwindow上设置焦点,则除了无法使edittext获得焦点之外,一切都完美无缺。如果我在popup上设置setFocusable,则edittext不会立即获得焦点,我必须双击才能移除它。

尝试这个:pw.setFocusable(true); - Mohammad Ersan
已经设置好了。我必须在弹出窗口上将focusable设置为true,否则我也无法聚焦到我的edittext上。实际上,我已经找到了解决方法,并将发布我的结果。 - ryandlf
正是我想知道的,如何处理弹出窗口之外的点击事件并关闭它。 - anargund
2个回答

3

我认为您可能打开了不止一个弹出窗口。onTouch方法将至少在触摸按下和触摸抬起时被调用,也可能在其中有几个触摸移动。尝试检查(arg1.getAction() == MotionEvent.ACTION_UP),然后仅在这时显示该窗口。


我曾经遇到过同样的问题,并使用上述方法解决了它,就像这样;fragment.getView().setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == MotionEvent.ACTION_UP) { popupFinishFragment("警告","确定要退出吗?"); return true; } return false; } }); } - oguzhan

1

您的弹出窗口没有获取到父活动的上下文。 请尝试像这样编写代码,而不是使用 "final PopupWindow pw = new PopupWindow(view, pWidth, pHeight, false);"。

final PopupWindow pw = new PopupWindow(getApplicationContext());
pw.setContentView(view);
pw.setHeight(pHeight);
pw.setWidth(pWidth);
pw.setFocusable(false);

这简直不是事实。PopupWindow 需要一个活动上下文。 - whlk
PopUpWindow构造函数中,您可以使用Activity.getBaseContext()来提供基本上下文。这也有助于避免在传递对已覆盖onBackPressed()的活动的引用时,弹出窗口不会因按下返回按钮或在屏幕上任何位置按下而被解除。 - prometheuspk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接