I want to get the exact X and Y position where I longClicked in the wi开发者_运维技巧ndow, but I found there's no method in OnLongClickListener to do this.
Is this possible?
Or I have to listen by JavaScript in the webView? How?
You need to implement the method
public boolean onTouchEvent(MotionEvent event)
in your view and then you can see the coordinates of touching point calling the function event.getX() and event.getY() Then get this to set a couple variables that the onLongClick method can access. More info can be found here
The complete working code looks like this. You need to overwrite onTouchEvent, indeed...
public class SubWebView extends WebView {
private Point lastTouch;
public SubWebView (Context context) {
super(context);
...
super.setLongClickable(true);
setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
return handler.onLongClick(lastTouch.x, lastTouch.y);
}
});
}
@Override
public boolean onTouchEvent (MotionEvent ev) {
lastTouch = new Point((int) ev.getX(), (int) ev.getY()) ;
return super.onTouchEvent(ev);
}
Especially, for some reason, it does not work setting an OnTouchEventListener to capture the coordinate - I'd maybe like to know why.
精彩评论