How to determine if a touch event is in an EditText?

I can get find getX () and getY () will return a float. But how to determine if the TouchEvent e.get (), e.getY () coordinates are inside the borders of the EditText interface element? I noticed that getX () and getY () are float, but getHeight () and getWidth () are int. Not going to help in comparison ...

+8
android
source share
1 answer

What exactly do you want to do? If you want to determine if your EditText is affected, add OnTouchListener to EditText ... or even OnClickListener.

Edit: if you want to detect outside, you can detect a touch event in the containing view, and then if you have an EditText view:

Rect editTextRect = new Rect(); myEditText.getHitRect(editTextRect); if (!editTextRect.contains((int)event.getX(), (int)event.getY())) { Log.d("test", "touch not inside myEditText"); } 

Or you add a touch listener both in EditText and in the container and return false in one of the EditText, so it will be intercepted and not be redirected to the parent. This way, all touches that you find in the parent listener will not belong to EditText.

+29
source share

All Articles