Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android,附加guesture侦听器_Java_Android_Class_View_Listener - Fatal编程技术网

Java Android,附加guesture侦听器

Java Android,附加guesture侦听器,java,android,class,view,listener,Java,Android,Class,View,Listener,我编写了手势检测器类,没有尝试添加手势监听器,当我在活动主视图的onCreate()函数中调用它时,它工作正常,但我无法通过单击“创建”按钮将其附加到不可见视图这是我的代码 我没有收到错误,它只是不侦听,我还可以在invisibleView类中设置onTouchListener() 主要 这里是我创建系统覆盖视图的类: public class InvisibleView extends Service { View myView; @Override public I

我编写了
手势检测器类
,没有尝试添加手势监听器,当我在
活动主视图的
onCreate()
函数中调用它时,它工作正常,但我无法通过单击“创建”按钮将其附加到不可见视图
这是我的代码

我没有收到错误,它只是不侦听,我还可以在
invisibleView类中设置
onTouchListener()

主要

这里是我创建
系统覆盖视图的类

public class InvisibleView extends Service {

    View myView;
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    public void onCreate(Context myContext) {
       super.onCreate();

       LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
       myView = new View(myContext);
       myView = inflater.inflate(R.layout.invisibleviewxml, null);

       WindowManager.LayoutParams params = new WindowManager.LayoutParams(
               WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
               WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
               PixelFormat.TRANSLUCENT);
       WindowManager wm = (WindowManager) myContext.getSystemService(WINDOW_SERVICE);
        wm.addView(myView, params);

          /* 
          ----------------------------------------------------- 
           this also works,the system overlay catches clicks
          ----------------------------------------------------- 
        myView.setOnTouchListener( new OnTouchListener() {
            @Override
            public boolean onTouch(View inviView, MotionEvent event) {
                Log.d("Gesture",  "simple touch from InvisibleView Class");
                return true;
            }

        });
          ----------------------------------------------------- 
           this does not work
          -----------------------------------------------------  

          myView.setOnTouchListener(new ClassGesturesDetection() {
       public void returnSingleTapUp() {
            Log.d ("Gesture from transparent", "single tab");
        }

    });
          -----------------------------------------------------  */

    }

  @Override
    public void onDestroy() {
        super.onDestroy();
        if(myView != null)
        {
            ((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(myView);
            myView = null;
        }
    }

}
下面是我的手势分类:

public class ClassGesturesDetection implements OnTouchListener {

     @SuppressWarnings("deprecation")
        private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());

        public boolean onTouch(final View view, final MotionEvent motionEvent) {
            return gestureDetector.onTouchEvent(motionEvent); 
        }

        private 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) {
                //Log.d("class Gesture", "on Down");
                return true;
            }

            @Override
          public void onLongPress(MotionEvent e) {
                returnOnLongPress();
             }

            @Override
          public boolean onSingleTapUp(MotionEvent e) {  
                returnSingleTapUp();
                return false;
          }



            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                boolean result = false;
                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();
                            }
                        }
                    } else {
                        if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                            if (diffY > 0) {
                                onSwipeBottom();
                            } else {
                                onSwipeTop();
                            }
                        }
                    }
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
                return result;
            }
        }

        public void onSwipeRight() {
        }

        public void onSwipeLeft() {
        }

        public void onSwipeTop() {
        }

        public void onSwipeBottom() {
        }

        public void returnSingleTapUp() {
        }
        public void returnOnLongPress() {
        }
    }

您必须添加此权限

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

你可以跟着
我在那里回答了你的问题

谢谢,我已经添加了,问题不是系统警报视图,问题是将guesture listener添加到此视图。是的,我在跟踪链接时也遇到了问题。谢谢,但我认为问题不是我无法从系统覆盖接收任何touchevents,这是可行的。我认为问题在于这一行:viewToWatch.setOnTouchListener(newclassgesturesdetection())
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>