Android PopupWindow不会被解雇

时间:2011-08-20 20:09:19

标签: android popupwindow dismiss

我有以下代码创建一个包含EditText的PopupWind:

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运行两次,我不能。我在这里缺少什么?

编辑:我稍微更新了我的代码。如果我不把注意力集中在弹出窗口上,除了我无法专注于我的编辑文本之外,一切都很完美。如果我在弹出窗口中执行setFocusable,则edittext不会立即获得焦点,我必须双击才能删除它。

2 个答案:

答案 0 :(得分:3)

我认为您可能会打开多个弹出窗口。 onTouch方法将被调用至少用于触摸和触摸,并且可能还有一些触摸移动。尝试检查(arg1.getAction()== MotionEvent.ACTION_UP)并仅显示窗口。

答案 1 :(得分:1)

您的弹出窗口未获取父活动的上下文。 尝试这样做而不是“最终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);