java 如何为整个屏幕设置 OnTouchListener?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17773835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set OnTouchListener for the entire screen?
提问by Lemon Juice
Please have a look at the following code. I am only posting the important sections of the code.
请看下面的代码。我只发布代码的重要部分。
activity_game.xml
活动游戏.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>
Game.java
游戏.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;
}
};
}
OnSwipTouchListener.java
OnSwipTouchListener.java
Note - I do not own this class of the code. It is written by another SO member.
注意 - 我不拥有此类代码。它是由另一个 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;
}
}
Now, I am trying to make the make the WHOLE ACTIVITY touched enabled. Please view Game.java
to see how I did it. But the problem is, it is not reacting to any kind of touch event! There is nothing wrong with the code OnSwipTouchListener.java
because I have used the touch events of this by setting the listener to an image view
.
现在,我正在尝试启用整个 ACTIVITY 触摸功能。请查看Game.java
我是如何做到的。但问题是,它不会对任何类型的触摸事件做出反应!代码没有任何问题,OnSwipTouchListener.java
因为我通过将侦听器设置为image view
.
There must be a problem where I am trying to make the whole activity touch enabled. By saying "Whole Activity Touch Enabled" what I mean is, I need to swipe across the whole screen instead of adding the listener to an imageview
.
一定有问题,我试图使整个活动触摸启用。通过说“启用整个活动触摸”,我的意思是,我需要在整个屏幕上滑动而不是将侦听器添加到imageview
.
What have I done wrong here?
我在这里做错了什么?
回答by Piseth Sok
This is my example:
这是我的例子:
@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;
}
Hope this is helpful to someone! Cheer!
希望这对某人有帮助!欢呼!
回答by Dreamer
As requested by the Questioner, the solution is at the answers to this question ontouchlistener-doesnt-work-with-relative-layout
根据提问者的要求,解决方案是在这个问题上的答案ontouchlistener-doesnt-work-with-relative-layout
回答by Gabe Sechan
Add the TouchListener to the highest level layout of your activity. That will catch any touches not handled elsewhere.
将 TouchListener 添加到您的活动的最高级别布局。这将捕获其他地方未处理的任何触摸。
回答by Mena
Is your RelativeLayout focusable?
你的 RelativeLayout 是可聚焦的吗?
android:focusable="true"
and/or
和/或
android:focusableInTouchMode="true"