OnClickListener - 事件的x,y位置?位置、事件、OnClickListener

由网友(出神入化早漆黑倨傲の眼眸)分享简介:我从视图派生的自定义视图。我想通知视图被点击时,与X,这里的点击发生的y位置。同样为长的点击。 I have a custom view derived from View. I'd like to be notified when the view is clicked, and the x,y location...

我从视图派生的自定义视图。我想通知视图被点击时,与X,这里的点击发生的y位置。同样为长的点击。

I have a custom view derived from View. I'd like to be notified when the view is clicked, and the x,y location of where the click happened. Same for long-clicks.

看起来像要做到这一点,我需要覆盖的onTouchEvent()。有没有办法让X,从OnClickListener事件虽然不是?

Looks like to do this, I need to override onTouchEvent(). Is there no way to get the x,y location of the event from an OnClickListener instead though?

如果不是,有什么讲,如果一个动作事件是一个'真正的'点击VS长单击等的好方法?该的onTouchEvent产生快速连续等多项活动。

If not, what's a good way of telling if a motion event is a 'real' click vs a long-click etc? The onTouchEvent generates many events in rapid succession etc.

感谢

推荐答案

感谢您。这正是我一直在寻找。我的code现在是:

Thank you. That was exactly what I was looking for. My code now is:

imageView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN){
                textView.setText("Touch coordinates : " +
                        String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
            }
            return true;
        }
    });

这的确precisely什么马克问的...

which does precisely what Mark asked for...

阅读全文

相关推荐

最新文章