简体   繁体   中英

Detect what view was clicked/touched below a alertdialog

I'm currently trying to enhance a android I've recently been given as a handover from a intern.

Cutting to the case, what I'm trying to achieve is when a error occurs for example invalid login a custom alertdialog is displayed - this is working fine. Alert is shown at the bottom of the screen and stays till the screen is touched. The problem is that I would like the focus to be set to what the user touches, for example the user may touch the username input or the password input. The focus should get requested by said view.

But I cannot work out how to detect what view is being pressed 'below' the alert dialog.

As you yourself hinted in a comment to the question, it's possible to find all views that are at a certain (x,y).

The drawing rect of a view can be found from View#getDrawingRect(android.graphics.Rect) , you might need to compensate for view scrolling though (eg ScrollView, ListView).

You can find the correct view by getting the root view, and then going through all views, and sub views, and subviews-subviews, etc.

Use findViewById to find a suitable root view (for an activity) or keep a reference to the fragment root view when you do onCreateView.

Be careful so that you select the deepest possible view since that is laid out on top.

But as @tyczj said, Android users are used to the case where clicking outside an alert dialog doesn't affect the view below.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM