OnClickListener - x,y location of event?

后端 未结 4 2216
难免孤独
难免孤独 2020-11-28 09:28

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.

L

4条回答
  •  不知归路
    2020-11-28 10:09

    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;
            }
        });
    

    which does precisely what Mark asked for...

提交回复
热议问题