可聚焦的弹出窗口与可聚焦的背景

时间:2011-10-04 07:00:51

标签: android popupwindow

我需要用几个按钮创建一个弹出窗口。按钮需要是可点击的,所以我将popupWindow属性设置为focusable。但是当我在弹出窗口外触摸时,弹出窗口就被解雇了。弹出窗口与EditText相关联。我的要求是,即使弹出窗口可见,用户也必须能够输入editText。

                 pWindow = new PopupWindow(context);
    pWindow.setBackgroundDrawable(new BitmapDrawable());
    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    popupView = (RelativeLayout) inflater.inflate(R.layout.popup, null);
    popupView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    pWindow.setContentView(popupView);
    pWindow.setWidth(popupView.getLayoutParams().width);
    pWindow.setHeight(popupView.getLayoutParams().height);
    pWindow.setFocusable(true);
    pWindow.setTouchable(true);
                 pWindow.showAsDropDown(anchor, 0, 0);

我尝试了各种组合,但无法达到预期效果。

1 个答案:

答案 0 :(得分:1)

为了让您的窗口在外部单击时停止关闭,您需要删除此行:

pWindow.setBackgroundDrawable(new BitmapDrawable());

不确定原因究竟是什么,但我知道制作背景可绘制设置将其设置为在外部点击时关闭。

现在至于制作窗口本身,你应该按顺序:

inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
popupView =(RelativeLayout) inflater.inflate(R.layout.popup, (ViewGroup) findViewById(R.id.<IdOfLayoutInPopupXML>);
pWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT,  LayoutParams.WRAP_CONTENT, true);
pWindow.setTouchable(true);
pWindow.showAsDropDown(anchor, 0, 0);

我知道这已经老了,但至少我会如何拥有它,因为这就是我的弹出窗口现在的工作方式。 (除了我不显示下拉菜单)。