Android Click Through PopupWindow

前端 未结 2 1945
没有蜡笔的小新
没有蜡笔的小新 2021-01-02 07:19

I have created a class that extends popupwindow. its constructor looks something like the following

super(builder.context.get());

this.setWindowLayoutMode(V         


        
相关标签:
2条回答
  • 2021-01-02 07:50
    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();
         }
       });
    }
    }
    

    try this code may be it help you . .. . . or may be this link will help u more .. http://mrbool.com/how-to-implement-popup-window-in-android/28285

    0 讨论(0)
  • 2021-01-02 07:51

    SOLUTION

    Register a touch interceptor for the Popupwindow. If you want the activity to handle it, assuming you have a reference to the activity, call someActivity.dispatchTouchEvent(someMotionEventPassedIntoTheTouchIntercepter);, if you want the popupwindow to handle the touch even, call someView.dispatchTouchEvent(theMotionEventPassedToTouchInterceptor) to the popupwindow's content view, as set in the popupwindows setContentView(someView) method.

    My method specifically looked like the following

    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;
        }
    };
    
    0 讨论(0)
提交回复
热议问题