Android通过PopupWindow点击

我创建了一个扩展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);

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

要实际显示popupwindow,我调用SomePopupWindow.showAtLocation(SomeActivity.getWindow().getDecorView().getRootView(), Gravity.CENTER, 0, 0);

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

胡说

touchListener正在响应,但输入不会被发送到基础活动或弹出窗口? (该按钮没有响应,就好像我要删除ontouchlistener一样,按钮工作正常)。

更新设置触摸监听器到frameLayout(即在这种情况下popupwindow的内容视图)而不是弹出窗口本身允许我点击弹出窗口中定义的按钮。 仍在寻找将触摸事件委托给基础活动/视图的方式

为Popupwindow注册一个触摸拦截器。 如果您希望活动处理它,假设您有对该活动的引用,请调用someActivity.dispatchTouchEvent(someMotionEventPassedIntoTheTouchIntercepter); ,如果您希望popupwindow处理触摸,可以调用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;
    }
};

为Popupwindow注册一个触摸拦截器。 如果您希望活动处理它,假设您有对该活动的引用,请调用someActivity.dispatchTouchEvent(someMotionEventPassedIntoTheTouchIntercepter); ,如果您希望popupwindow处理触摸,可以调用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;
    }
};

public class TestPopupActivity extends Activity {

//The "x" and "y" position of the "Show Button" on screen.
Point p;

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

   Button btn_show = (Button) findViewById(R.id.show_popup);
   btn_show.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View arg0) {

       //Open popup window
       if (p != null)
       showPopup(TestPopupActivity.this, p);
     }
   });
}

// Get the x and y position after the button is draw on screen
// (It's important to note that we can't get the position in the onCreate(),
// because at that stage most probably the view isn't drawn yet, so it will return (0, 0))
@Override
public void onWindowFocusChanged(boolean hasFocus) {

   int[] location = new int[2];
   Button button = (Button) findViewById(R.id.show_popup);

   // Get the x, y location and store it in the location[] array
   // location[0] = x, location[1] = y.
   button.getLocationOnScreen(location);

   //Initialize the Point with x, and y positions
   p = new Point();
   p.x = location[0];
   p.y = location[1];
}

// The method that displays the popup.
private void showPopup(final Activity context, Point p) {
   int popupWidth = 200;
   int popupHeight = 150;

   // Inflate the popup_layout.xml
   LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
   LayoutInflater layoutInflater = (LayoutInflater) context
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);

   // Creating the PopupWindow
   final PopupWindow popup = new PopupWindow(context);
   popup.setContentView(layout);
   popup.setWidth(popupWidth);
   popup.setHeight(popupHeight);
   popup.setFocusable(true);

   // Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
   int OFFSET_X = 30;
   int OFFSET_Y = 30;

   // Clear the default translucent background
   popup.setBackgroundDrawable(new BitmapDrawable());

   // Displaying the popup at the specified location, + offsets.
   popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);

   // Getting a reference to Close button, and close the popup when clicked.
   Button close = (Button) layout.findViewById(R.id.close);
   close.setOnClickListener(new OnClickListener() {

     @Override
     public void onClick(View v) {
       popup.dismiss();
     }
   });
}
}

试试这个代码可能会帮助你。 ..。 。 或者可能是这个链接会帮助你更多.. http://mrbool.com/how-to-implement-popup-window-in-android/28285

链接地址: http://www.djcxy.com/p/19153.html

上一篇: Android Click Through PopupWindow

下一篇: matrices with lowest difference between their sum