Android: custom view onClickEvent with X & Y locations

后端 未结 6 636
你的背包
你的背包 2020-12-21 15:12

I\'ve got a custom view and I want to get the X and Y coordinates of a user click. I\'ve found that I can only get the coordinates from the onTouchEvent and an onClickEvent

6条回答
  •  天涯浪人
    2020-12-21 15:54

    Now, I don't know about onTouchEvent short circuiting onClick, and I don't know if maybe you need to set android:clickable to true (or setClickable(true)). But, if you can't make it work cleanly, you can use a boolean in the onTouchEvent to mimic click detection if all else fails. in ACTION_DOWN, set it to true (isClicking or something like that), in ACTION_MOVE set it to false (because the user is dragging therefore not clicking) and then in ACTION_UP you can test for it. If it's true, this means the user has pressed down but not dragged. It's hacky, but if you can't get onClick to work it's basically the same thing.

提交回复
热议问题