How do I detect if a touch event has landed within an EditText?

后端 未结 1 755
渐次进展
渐次进展 2021-02-05 17:20

I can get find getX(), and getY() both return a float. But how do I detect if the coordinates of a TouchEvent e.get(), e.getY() are within the boundaries of the EditText UI ele

相关标签:
1条回答
  • 2021-02-05 17:44

    What exactly do you want to do? If you only want to detect if your EditText is touched, add an OnTouchListener to the EditText... or even OnClickListener.

    Edit: If you want to detect outside, you can detect touch event in the containing view, and then, given you have your 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 to the EditText and the container, and return false in the one of the EditText, this way it will be intercepted and not forwarded to the parent. So, all the touches you detect in the listener of the parent, will not belong to the EditText.

    0 讨论(0)
提交回复
热议问题