【问题标题】:Java Android, attach guesture listenerJava Android,附加来宾监听器
【发布时间】:2013-12-09 22:52:27
【问题描述】:

我写了Gesture Detector class 并且没有尝试添加手势侦听器,当我在activity_main 视图的onCreate() 函数中调用它时它工作正常,但我无法将它附加到我创建的不可见视图通过按钮单击..
这是我的代码,

我没有收到任何错误,它只是不听,我也可以在invisibleView class中设置一个onTouchListener()..

主要

public class MainActivity extends Activity {

    String TAG = "myTAG";
    SocketClass mySocketClass;
    View viewToWatch;
    InvisibleView myInvisibleView;
    Context myContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
           savedInstanceState = new Bundle();
           myInvisibleView = new InvisibleView();

           /* 
          ----------------------------------------------------- 
           this works, when i add the listener to the main view:
          ----------------------------------------------------- 

           viewToWatch= (View) findViewById(R.id.mainViewId);
           viewToWatch.setOnTouchListener(new ClassGesturesDetection() {

               public void returnSingleTapUp() {
                    Log.d ("Gesture from main View", "single tab");
                }              
            });
        ----------------------------------------------------- 
        */ 

    }


  public void startApp(View activity_main_view){
        myInvisibleView.onCreate(this.getApplicationContext());
        putOnTochListenerToInvsibleView();
    }


    public void putOnTochListenerToInvsibleView( ) {

        /* 
          ----------------------------------------------------- 
           there i want to add the listener:
          ----------------------------------------------------- 
        */
         setContentView(R.layout.invisibleviewxml);
           viewToWatch= (View) findViewById(R.id.invisibleViewId);
           viewToWatch.setOnTouchListener(new ClassGesturesDetection() {
                   public void returnSingleTapUp() {
                    Log.d ("Gesture from transparent view", "single tab");
                }
            });
    }
}

这里是我创建system overlay view的类:

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() {
        }
    }

【问题讨论】:

    标签: java android class view listener


    【解决方案1】:

    您必须添加此权限

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

    你可以关注this 我已经回答了你的问题

    【讨论】:

    • 谢谢,我已经添加了,问题不是系统警报视图,问题是在这个视图中添加了guesture listener
    • 是的,一旦点击链接,我也遇到了这个问题
    • 谢谢,但我认为问题不在于我无法从系统叠加层接收任何触摸事件,这是可行的。我认为问题出在这一行:viewToWatch.setOnTouchListener(new ClassGesturesDetection()
    猜你喜欢
    • 2012-04-29
    • 1970-01-01
    • 1970-01-01
    • 2022-09-28
    • 2018-10-25
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多