这里是我的代码,我想检测时,我的手指下降的屏幕,所以当我触摸屏幕,我检测ACTION_DOWN
但是当我走下屏幕,我的手指, ACTION_MOVE
不认可,也不ACTION_UP
你知道为什么吗?
float x=0;
protected void onCreate(Bundle savedInstanceState) {
do things
ImageView image2 = (ImageView) findViewById(R.id.imageView3);
image2.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if (arg1.getAction()==MotionEvent.ACTION_DOWN) {
x=arg1.getX();
}
else {
if (arg1.getAction()==MotionEvent.ACTION_MOVE){
if (arg1.getX()>x) {
do things
}
}
else {
if (arg1.getAction()==MotionEvent.ACTION_UP){
do things
}
}
}
}
If your onTouch()
method returns false
in response to the initial ACTION_DOWN
MotionEvent
, it will not receive any of the subsequent events that belong to this particular gesture. Instead those touch events will be presented to the parent in the hierarchy.
To phrase that another way, if you return false
from onTouch()
during the start of a gesture (the ACTION_DOWN
), it signals that the method no longer wants to see any more of the gesture, and that the gesture's events should go to the parent View
.
As markproxy points out in the comments below, returning false
when the MotionEvent
is anything other than an ACTION_DOWN
, such as an ACTION_MOVE
for example, will not prevent subsequent MotionEvent
s in the current gesture being presented to the View
.
MotionEvent.getAction()
返回不仅仅是标志更多。 尝试以下方法:
int action = arg1.getAction() & MotionEvent.ACTION_MASK;
if (action == MotionEvent.ACTION_MOVE) ...
您还可以使用MotionEvent.getActionMasked()
又见http://developer.android.com/reference/android/view/MotionEvent.html
事情的情侣:
1)你必须返回一个布尔值,以显示你视图所消耗的事件。
这里是一个非常简单的实现,其工作原理:
package com.test;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class TestProjActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String TAG = "TEST_TAG";
View v = findViewById(R.id.touchTest);
v.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_DOWN) {
Log.e(TAG,"Down");
return true;
}
if (event.getAction()==MotionEvent.ACTION_MOVE){
Log.e(TAG,"Move");
return true;
}
if (event.getAction()==MotionEvent.ACTION_UP){
Log.e(TAG,"Up");
return true;
}
return false;
}
});
}
}
下面是main.xml中
::
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/touchTest"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
有两种情况:
1)如果您还设置OnClickListener() - > onTouch()应该返回false。
(如果onTouch()返回true,OnClickListener()将无法正常工作)
2)如果您没有设置OnClickListener() - > onTouch()应返回true。
(如果onTouch()返回false,只有ACTION_DOWN叫)