GestureDetector不起作用(来自android开发者的例子)

我正在阅读在Android开发人员在Android的手势,并按照教程我试图运行以下代码:

public class MainActivity extends Activity implements OnGestureListener, OnDoubleTapListener { private static final String DEBUG_TAG = "Gestures"; private GestureDetectorCompat mDetector; // Called when the activity is first created. @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); View v = new RelativeLayout(this); v.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); setContentView(v); mDetector = new GestureDetectorCompat(this, this); mDetector.setOnDoubleTapListener(this); } @Override public boolean onTouchEvent(MotionEvent event) { this.mDetector.onTouchEvent(event); return super.onTouchEvent(event); } @Override public boolean onDown(MotionEvent event) { Log.d(DEBUG_TAG, "onDown: " + event.toString()); return true; } @Override public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) { Log.d(DEBUG_TAG, "onFling: " + event1.toString() + event2.toString()); return true; } @Override public void onLongPress(MotionEvent event) { Log.d(DEBUG_TAG, "onLongPress: " + event.toString()); } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { Log.d(DEBUG_TAG, "onScroll: " + e1.toString() + e2.toString()); return true; } @Override public void onShowPress(MotionEvent event) { Log.d(DEBUG_TAG, "onShowPress: " + event.toString()); } @Override public boolean onSingleTapUp(MotionEvent event) { Log.d(DEBUG_TAG, "onSingleTapUp: " + event.toString()); return true; } @Override public boolean onDoubleTap(MotionEvent event) { Log.d(DEBUG_TAG, "onDoubleTap: " + event.toString()); return true; } @Override public boolean onDoubleTapEvent(MotionEvent event) { Log.d(DEBUG_TAG, "onDoubleTapEvent: " + event.toString()); return true; } @Override public boolean onSingleTapConfirmed(MotionEvent event) { Log.d(DEBUG_TAG, "onSingleTapConfirmed: " + event.toString()); return true; } } 

代码来自这里的网站。

但是当我运行应用程序时,我点击或长按视图时没有debugging信息。

顺便说一句,我testing在模拟器和HTC E1设备的应用程序。

问题是什么?

Solutions Collecting From Web of "GestureDetector不起作用(来自android开发者的例子)"

你正在创build一个手势检测器,但是你永远不会把它挂在你的视图上。 尝试改变你的onCreate像这样:

 super.onCreate(savedInstanceState); View v = new RelativeLayout(this); v.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); setContentView(v); mDetector = new GestureDetectorCompat(this, this); mDetector.setOnDoubleTapListener(this); v.setOnTouchListener(new OnTouchListener(){ public boolean onTouch(View v, MotionEvent me){ return mDetector.onTouchEvent(me); } }); 

迟到了,但是我解决这个问题的方法是:

 @Override public boolean onTouchEvent(MotionEvent event) { this.mDetector.onTouchEvent(event); return super.onTouchEvent(event); } 

探测器的回报消失了。 改成:

 @Override public boolean onTouchEvent(MotionEvent event) { if (this.mDetector != null) return this.mDetector.onTouchEvent(event); return super.onTouchEvent(event); } 

但这是一个很好的解决scheme吗? Android内幕的时间。 ;)

Tschau Fred

我编辑了你的第一行:

 public class MainActivity extends Activity implements OnGestureListener, OnDoubleTapListener { 

我用你的代码,但提供了完整的接口名称,如下所示:

 public class MainActivity extends Activity implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener { 

这在编辑之后工作。 希望这可以帮助。

这里是简单的Android代码的手势方向检测

MainActivity.java

 import java.util.ArrayList; import android.app.Activity; import android.gesture.Gesture; import android.gesture.GestureLibraries; import android.gesture.GestureLibrary; import android.gesture.GestureOverlayView; import android.gesture.GestureOverlayView.OnGesturePerformedListener; import android.gesture.GestureStroke; import android.gesture.Prediction; import android.os.Bundle; import android.widget.Toast; public class MainActivity extends Activity implements OnGesturePerformedListener { GestureOverlayView gesture; GestureLibrary lib; ArrayList<Prediction> prediction; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lib = GestureLibraries.fromRawResource(MainActivity.this, R.id.gestureOverlayView1); gesture = (GestureOverlayView) findViewById(R.id.gestureOverlayView1); gesture.addOnGesturePerformedListener(this); } @Override public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) { // TODO Auto-generated method stub ArrayList<GestureStroke> strokeList = gesture.getStrokes(); // prediction = lib.recognize(gesture); float f[] = strokeList.get(0).points; String str = ""; if (f[0] < f[f.length - 2]) { str = "Right gesture"; } else if (f[0] > f[f.length - 2]) { str = "Left gesture"; } else { str = "no direction"; } Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show(); } } 

activity_main.xml中

 <android.gesture.GestureOverlayView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:android1="http://schemas.android.com/apk/res/android" xmlns:android2="http://schemas.android.com/apk/res/android" android:id="@+id/gestureOverlayView1" android:layout_width="match_parent" android:layout_height="match_parent" android1:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Draw gesture" android:textAppearance="?android:attr/textAppearanceMedium" /> </android.gesture.GestureOverlayView>