This is because the popup window does not respond to onTouch or onKey events unless it has a background that != null. Check out some code I wrote to help with this. In the basic case you can to callPopupWindow#setBackgroundDrawable(new BitmapDrawable()) to force it to act the way you expect. You won't need your own onKey listener. You might also need to callPopupWindow#setOutsideTouchable(true) if you want it to go away when the user clicks outside of the window boundaries.
Extended esoteric answer:
The reason the background cannot be null is because of what happens inPopupWindow#preparePopup. If it detects background != null it creates an instance ofPopupViewContainer and calls setBackgroundDrawable on that and puts your content view in it.PopupViewContainer is basically a FrameLayout that listens for touch events and theKeyEvent.KEYCODE_BACK event to dismiss the window. If background == null, it doesn't do any of that and just uses your content view. You can, as an alternative to depending on PopupWindow to handle that, extend your root ViewGroup to behave the way you want.
PopupWindow pw;
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.weight_popup, (ViewGroup)findViewById(R.id.linlay_weight_popup));
pw = new PopupWindow(layout,LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT, true);
pw.setBackgroundDrawable(new BitmapDrawable());
pw.setOutsideTouchable(true);
pw.showAsDropDown(btnSelectWeight);
from: http://stackoverflow.com/questions/3121232/android-popup-window-dismissal
本文探讨了在Android开发中遇到的PopupWindow不响应触摸和按键事件的问题,并提供了一个解决方案。通过设置背景图和使PopupWindow可外部触碰,可以确保PopupWindow在用户点击窗外时关闭。
533

被折叠的 条评论
为什么被折叠?



