How does PopupWindow receive a notification of a touch of external events?

The documentation states that PopupWindow will be informed of touch events outside its window when setOutsideTouchable(boolean touchable) set to true. How is a popup reported? I don't see any listeners like setOnOutsideTouchListener etc. to receive this information.

Example

 PopupWindow popup = new PopupWindow(); popup.setOutsideTouchable(true); //now what..how to receive those touch outside events? 

Thanks.

+6
source share
5 answers

try using setTouchInterceptor as the following snapshot

 popup.setTouchInterceptor(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_OUTSIDE) { popup.dismiss(); return true; } return false; } }); 

also remember to set the following flag:

 popup.setOutsideTouchable(true); 
+7
source

When you touch the PopupWindow outer panel, the OnDismissListener starts up as the touch of the outer window fires the default window, so you can set OnDismissListener to popupWindow to listen to the touch outside the window.

 popup.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { //Do Something here } }); 
+1
source

"The popup is a floating container that appears on top of the current activity."

 See: https://developer.android.com/reference/android/widget/PopupWindow.html 

I think this is what you are looking for.

0
source

PopupWindow is notified of external touch events in the same way as all other touch events. When the flag is set outside the events redirected to the popup, and you can handle them the same way you handle touches. There is no special method for checking this external event or a set of listeners for such events. If you check the source code:

 1341 @Override 1342 public boolean dispatchTouchEvent(MotionEvent ev) { 1343 if (mTouchInterceptor != null && mTouchInterceptor.onTouch(this, ev)) { 1344 return true; 1345 } 1346 return super.dispatchTouchEvent(ev); 1347 } 1348 1349 @Override 1350 public boolean onTouchEvent(MotionEvent event) { 1351 final int x = (int) event.getX(); 1352 final int y = (int) event.getY(); 1353 1354 if ((event.getAction() == MotionEvent.ACTION_DOWN) 1355 && ((x < 0) || (x >= getWidth()) || (y < 0) || (y >= getHeight()))) { 1356 dismiss(); 1357 return true; 1358 } else if (event.getAction() == MotionEvent.ACTION_OUTSIDE) { 1359 dismiss(); 1360 return true; 1361 } else { 1362 return super.onTouchEvent(event); 1363 } 1364 } 

Now you can see that PopupWindow itself checks that the X / Y event is outside and is rejected. That way, you can set the TouchInterceptor to capture the event before or instead of the default handler. Or you can override onTouchEvent to do your own event handling and call super if that makes sense to you.

0
source

补充: In my case, if I press EditText outside popupWindow, If setFocusable (true), event.getAction () == 0 (Therefore, I need to double-click editText to enter). If setFocusable (false, event.getAction () == 4 (MotionEvent.ACTION_OUTSIDE). (Click once and I can enter)

0
source

Source: https://habr.com/ru/post/976950/


All Articles