Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.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 如何为整个屏幕设置OnTouchListener?_Java_Android_Android Activity_Touch_Ontouchlistener - Fatal编程技术网

Java 如何为整个屏幕设置OnTouchListener?

Java 如何为整个屏幕设置OnTouchListener?,java,android,android-activity,touch,ontouchlistener,Java,Android,Android Activity,Touch,Ontouchlistener,请看一下下面的代码。我只是发布代码的重要部分 活动\u game.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fullView" android:layout_width="match_parent" android:l

请看一下下面的代码。我只是发布代码的重要部分

活动\u game.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fullView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Game" >

    <TextView
        android:id="@+id/numberOfQuestionsLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:text="TextView" />
</RelativeLayout>
OnSwipTouchListener.java

public class Game extends Activity {



    private View fullView;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        fullView = (View)findViewById(R.id.fullView);
        fullView.setOnTouchListener(imageViewSwiped);



    }


 OnTouchListener imageViewSwiped = new OnSwipeTouchListener()
    {
         public boolean onSwipeRight() 
         {
            //code removed
         }

          public boolean onSwipeLeft() 
          {
             //code removed
                return true;
          }

          public boolean onSwipeBottom()
          {
              //code removed
              return true;
          }


    };
}
注意-我不拥有此类代码。它是由另一位SO成员编写的

  package game.Game;
    import android.view.GestureDetector;
    import android.view.GestureDetector.SimpleOnGestureListener;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnTouchListener;

    public class OnSwipeTouchListener implements OnTouchListener {

        private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());

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

        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) {
                return super.onDown(e);
            }

            @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) {
                                result = onSwipeRight();
                            } else {
                                result = onSwipeLeft();
                            }
                        }
                    } else {
                        if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                            if (diffY > 0) {
                                result = onSwipeBottom();
                            } else {
                                result = onSwipeTop();
                            }
                        }
                    }
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
                return result;
            }
        }

        public boolean onSwipeRight() {
            return false;
        }

        public boolean onSwipeLeft() {
            return false;
        }

        public boolean onSwipeTop() {
            return false;
        }

        public boolean onSwipeBottom() {
            return false;
        }
    }
现在,我正在努力使整个活动被激活。请查看
Game.java
以了解我是如何做到的。但问题是,它不会对任何触摸事件做出反应!代码
OnSwipTouchListener.java
没有什么问题,因为我通过将监听器设置为
图像视图来使用它的触摸事件

在我试图启用整个活动触控功能时,一定存在问题。我说“全活动触控启用”的意思是,我需要在整个屏幕上滑动,而不是将侦听器添加到
imageview


我做错了什么?

将TouchListener添加到活动的最高级别布局中。这将捕捉任何其他地方未处理的触摸。

您的相对焦度可调吗

android:focusable="true"
和/或

android:focusableInTouchMode="true"
根据提问者的要求,解决方案是在回答这个问题时给出的这是我的示例:

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        float x = event.getX();
        float y = event.getY();
        Toast.makeText(Example.this, "x=" + x + " y="+ y,
                Toast.LENGTH_SHORT).show();
        return false;
    }

希望这对别人有帮助!加油

谢谢你的回复。最高级别的布局是相对布局,我在中使用过code@Dreamer:该部分已在我的代码中。请查看“fullView”id和变量。它指的是我发布的XML文件中的相对布局。事实并非如此working@Knight.. 这些可能会有帮助@梦想家:第三个环节就是答案。请提供答案:)呃,关于这个问题有什么想法吗?