2011-10-04 86 views
1

我需要制作一个带有几个按钮的弹出式窗口。按钮需要点击,所以我将popupWindow属性设置为可调焦。但只要我在popupWindow之外触摸,弹出窗口就会被解除。弹出窗口与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

为了让你的窗口停止关闭,当您点击之外,你需要删除这一行:

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); 

我知道这是旧的,但这个至少我怎么会拥有它,因为这是怎么我的弹出式窗口工作的权利现在。 (除了我不显示为下拉菜单)。

+0

谢谢。我会试试这个。同时,我已经通过在需要弹出窗口的位置创建viewstub并更改其可见性来实现它。 – rDroid