请注意:此问题是关于Android PopupWindow,而不是Dialog。
我正在尝试使Android PopupWindow成为非模态:
我已经在PopupWindow的内容视图中设置了触摸侦听器,以测试事件是否在PopupWindow之外,如果是,则返回false。我希望然后将MotionEvent传播到单击的视图。但似乎由于这些视图不在当前窗口之内,因此它们没有收到事件。
即使事件不在PopupWindow之外,如何将事件传播到单击的视图?
public class NonModalPopupWindow extends PopupWindow {
private static final String TAG = "NonModalPopupWindow";
public NonModalPopupWindow(View contentView, int width, int height) {
super(width, height);
setContentView(contentView);
contentView.setOnTouchListener(createOnTouchListener());
setFocusable(true);
}
private View.OnTouchListener createOnTouchListener() {
return new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int x = (int) event.getX();
final int y = (int) event.getY();
final boolean isTouchOutside = (x < 0) || (x >= getWidth()) || (y < 0) || (y >= getHeight());
if (isTouchOutside || event.getAction() == MotionEvent.ACTION_OUTSIDE) {
Log.e(TAG, "Touched OUTSIDE");
// How to propagate this event so that it's received by the View shown
// At the location where I clicked, outside of the PopupWindow?
return false;
} else {
Log.e(TAG, "Touched INSIDE");
return true;
}
}
};
}
}
尝试一下:
只是您不想使用活动或Theme.Dialog的DialogFragment冒险的补充