当我使用下面的代码时,它给出了一个错误“对于类型对象,方法onTouch(运动事件)是未定义的。”错误在“return super.onTouch(event);”行中。 我不知道它是什么。我的应用程序是关于检测webview上的手势。很好地检测到手势,但它禁用了webview的内置手势,这就是我添加这段代码以便可以检测到内置手势的原因。
webView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return super.onTouch(event);
}
});}
如果有人想要整个代码......这里是
package i.rock.fb;
import android.os.Bundle;
import android.app.Activity;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MenuInflater;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;
public class MainActivity extends Activity {
@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater blowup=getMenuInflater();
blowup.inflate(R.menu.main, menu);
return true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView=(WebView)findViewById(R.id.webView);
webView.setWebViewClient(new our());
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/www/index.html");
@SuppressWarnings("deprecation")
final GestureDetector gestureDetector = new GestureDetector(new GestureListener());
webView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return super.onTouch(event);
}
});}
final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;WebView webView=(WebView)findViewById(R.id.webView);
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {WebView webView=(WebView)findViewById(R.id.webView);
webView.loadUrl("http://www.google.com");
}
public void onSwipeLeft() {WebView webView=(WebView)findViewById(R.id.webView);
webView.loadUrl("http://www.google.com");
}}