Java 手势检测器不工作

Java 手势检测器不工作,java,android,gesturedetector,Java,Android,Gesturedetector,我有一套java代码,我试图检测用户的触摸手势。当用户进行简单的触摸/向下滑动等操作时,文本视图将显示用户当前所做的操作。然而,当我在模拟器上运行代码时,它只是一个显示Hello World的黑屏!当我触摸时,什么也看不见。。为什么会这样?附件是代码。谢谢你的帮助 package org.tp.iit.cds.BrailleTypeSend; import android.app.Activity; import android.app.AlertDialog; import android.

我有一套java代码,我试图检测用户的触摸手势。当用户进行简单的触摸/向下滑动等操作时,文本视图将显示用户当前所做的操作。然而,当我在模拟器上运行代码时,它只是一个显示Hello World的黑屏!当我触摸时,什么也看不见。。为什么会这样?附件是代码。谢谢你的帮助

package org.tp.iit.cds.BrailleTypeSend;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector;
import android.widget.TextView;
import android.graphics.Color;


public class BrailleSend extends Activity implements OnGestureListener {
    /** Called when the activity is first created. */
    public LinearLayout main;    
    public TextView viewA;

    public GestureDetector gestureScanner;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        gestureScanner = new GestureDetector(this);

        main = new LinearLayout(this);
        main.setBackgroundColor(Color.GRAY);
        main.setLayoutParams(new LinearLayout.LayoutParams(320,480));

        viewA = new TextView(this);
        viewA.setBackgroundColor(Color.YELLOW);
        viewA.setTextColor(Color.WHITE);
        viewA.setTextSize(16);
        viewA.setLayoutParams(new LinearLayout.LayoutParams(320,80));
        main.addView(viewA);

        setContentView(R.layout.main);
    }

    @Override
    public boolean onDown(MotionEvent e) {  
        viewA.setText("Down Stroke");
        return true;
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {       
        viewA.setText("tap");
        return true;
    }

} 
三件事:

  • 使用这段代码所要做的就是在堆上创建一堆对象并设置一些属性。您创建的这些视图对象甚至没有附加到视图层次中
  • 活动的视图继承权来自布局文件夹中的main.xml文件,因为您已经编写了:
    setContentView(R.layout.main)在您的代码中

  • 您尚未将GestureListener附加到任何内容。您希望如何调用回调

    • 三件事:

      • 使用这段代码所要做的就是在堆上创建一堆对象并设置一些属性。您创建的这些视图对象甚至没有附加到视图层次中
      • 活动的视图继承权来自布局文件夹中的main.xml文件,因为您已经编写了:
        setContentView(R.layout.main)在您的代码中

      • 您尚未将GestureListener附加到任何内容。您希望如何调用回调


      我不确定是否需要为GestureDetector添加手势覆盖,但我知道如果使用OnGeturePerformedListener,则需要在视图中添加手势覆盖。手势叠加充当用户的绘图板

      GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
      
      以及XML:

      <android.gesture.GestureOverlayView
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/gestures"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      
      android:gestureStrokeType="multiple"
      android:eventsInterceptionEnabled="true"
      android:orientation="vertical">
      
      <ListView
          android:id="@android:id/list"  
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent"  />
      

      我不确定是否需要为GestureDetector添加手势覆盖,但我知道如果使用OnGeturePerformedListener,则需要在视图中添加手势覆盖。手势叠加充当用户的绘图板

      GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
      
      以及XML:

      <android.gesture.GestureOverlayView
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/gestures"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      
      android:gestureStrokeType="multiple"
      android:eventsInterceptionEnabled="true"
      android:orientation="vertical">
      
      <ListView
          android:id="@android:id/list"  
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent"  />
      

      我也有同样的问题。在@Jonathan Low的代码中,他正在做“扩展活动”。但是,当我扩展了ImageView时,它就可以工作了。对上述代码的哪些更改将导致onSingleTapUp着火?我想我理解活动根不是视图、或其他东西,我也有同样的问题。在@Jonathan Low的代码中,他正在做“扩展活动”。但是,当我扩展了ImageView时,它就可以工作了。对上述代码的哪些更改将导致onSingleTapUp着火?我想我理解活动根不是视图、或其他东西