Android点击PopupWindow

rpe*_*yng 10 android z-order onclicklistener

我创建了一个扩展popupwindow的类.它的构造函数看起来如下所示

super(builder.context.get());

this.setWindowLayoutMode(ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT);
setFocusable(true);
setBackgroundDrawable(new BitmapDrawable());
setTouchInterceptor(onTouchListener);
FrameLayout frameLayout = new FrameLayout(builder.context.get());
LayoutInflater inflater = (LayoutInflater) builder.context.get().getSystemService(
        Context.LAYOUT_INFLATER_SERVICE);
View overlayBase = inflater.inflate(R.layout.tutorial_view_items, null, false);
frameLayout.addView(builder.backgroundView);
frameLayout.addView(overlayBase);
setContentView(frameLayout);
Run Code Online (Sandbox Code Playgroud)

onTouchListener如下所示:

private OnTouchListener onTouchListener = new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        Log.d(TAG, "Received");

        if (Math.pow(x - coordinates[0], 2) + Math.pow((y - coordinates[1]), 2) < Math.pow(radius, 2)) {
            Log.d(TAG, "bubbled through");
            return false;
        }
        return true;
    }
};
Run Code Online (Sandbox Code Playgroud)

我打电话来实际显示弹出窗口 SomePopupWindow.showAtLocation(SomeActivity.getWindow().getDecorView().getRootView(), Gravity.CENTER, 0, 0);

视觉代表,这是有问题的产品

胡说

touchListener正在响应,但是输入没有被发送到底层活动或弹出窗口?(按钮没有响应,就像我要移除ontouchlistener一样,按钮工作正常).

更新 将触摸侦听器设置为frameLayout(在本例中为popupwindow的内容视图),而不是弹出窗口本身允许我单击弹出窗口中定义的按钮.仍在寻找将触摸事件委托给基础活动/视图的方法

为Popupwindow注册触摸拦截器.如果您希望活动处理它,假设您有对活动的引用,请调用someActivity.dispatchTouchEvent(someMotionEventPassedIntoTheTouchIntercepter);,如果您希望弹出窗口甚至可以处理触摸,请调用someView.dispatchTouchEvent(theMotionEventPassedToTouchInterceptor)popupwindow的内容视图,如popupwindows setContentView(someView)方法中所设置的那样.

我的方法具体如下所示

private OnTouchListener onTouchListener = new OnTouchListener() {
@Override
    public boolean onTouch(View v, MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        if (Math.pow(x - coordinates[0], 2) + Math.pow((y - coordinates[1]), 2) < Math.pow(
                radius, 2)) {
            activity.get().dispatchTouchEvent(event);
            return false;
        }
        frameLayout.dispatchTouchEvent(event);
        return true;
    }
};
Run Code Online (Sandbox Code Playgroud)

rpe*_*yng 5

解决方案

为 Popupwindow 注册一个触摸拦截器。如果您希望 Activity 处理它,假设您有对 Activity 的引用,请调用someActivity.dispatchTouchEvent(someMotionEventPassedIntoTheTouchIntercepter);,如果您希望 popupwindow 甚至处理触摸,请调用someView.dispatchTouchEvent(theMotionEventPassedToTouchInterceptor)popupwindow 的内容视图(如 popupwindowssetContentView(someView)方法中设置)。

我的方法具体如下

private OnTouchListener onTouchListener = new OnTouchListener() {
@Override
    public boolean onTouch(View v, MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        if (Math.pow(x - coordinates[0], 2) + Math.pow((y - coordinates[1]), 2) < Math.pow(
                radius, 2)) {
            activity.get().dispatchTouchEvent(event);
            return false;
        }
        frameLayout.dispatchTouchEvent(event);
        return true;
    }
};
Run Code Online (Sandbox Code Playgroud)