webView onTouchEvent
i'm trying to catch the onTouchEvent of a WebView, in order to handle actions like
MotionEvent.ACTION_UP
, MOVE
and CANCEL
. I made a开发者_JAVA百科 simple example but did not success, however i succed with just a View. Am i missing something?
Thanks
public class HelloWebView extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyWebView(this));
}
private static class MyWebView extends WebView {
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
Log.w("TouchEvent","Touch" + action);
Log.w("HitResult",this.getHitTestResult().toString());
switch (action) {
case (MotionEvent.ACTION_DOWN): // Touch screen pressed
break;
case (MotionEvent.ACTION_UP): // Touch screen touch ended
break;
case (MotionEvent.ACTION_MOVE): // Contact has moved across screen
break;
case (MotionEvent.ACTION_CANCEL): // Touch event cancelled
break;
}
return super.onTouchEvent(event);
}
public MyWebView(Context context) {
super(context);
this.getSettings().setJavaScriptEnabled(true);
this.loadUrl("http://www.google.com");
}
}
}
it is because you are using a WebView, it has already handled much of the onTouchEvent movements. I haven't got an answer right now but at least I can tell you what the problem is!
精彩评论