Android:OnTouch,MotionEvent.ACTION_MOVE无法识别?

mor*_*org 26 android ontouchlistener actionevent

这是我的代码,我想检测我的手指在屏幕上的时间,所以当我触摸屏幕时,我检测到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
                    }
                }
            }
}
Run Code Online (Sandbox Code Playgroud)

Tre*_*vor 95

如果您的onTouch()方法返回false以响应初始值ACTION_DOWN MotionEvent,则它将不会接收属于此特定手势的任何后续事件.相反,这些触摸事件将呈现给层次结构中的父级.

为了短语,另一种方式,如果返回false从onTouch()手势(在开始时ACTION_DOWN),它标志着该方法不再希望看到更多的手势,那手势的事件应该去父母View.

如markproxy在下面的评论所指出的,在返回false时,MotionEvent是任何比一个其他ACTION_DOWN,诸如ACTION_MOVE例如,将不防止随后MotionEventS IN的当前姿势被呈现给View.

  • 我认为需要稍微改进才能使这个答案正确.如果你从`onTouch()`_返回`false`,当`MotionEvent`是一个'ACTION_DOWN` event_时,它表示`View`不再想再看到这个动作,并且手势的事件应该去父`ViewGroup`.例如,从"ACTION_MOVE"事件返回"false"将不会阻止将来的事件路由到此"View". (3认同)

Tha*_*lur 8

MotionEvent.getAction()返回的不仅仅是旗帜.请尝试以下方法:

int action = arg1.getAction() & MotionEvent.ACTION_MASK;
if (action == MotionEvent.ACTION_MOVE) ...
Run Code Online (Sandbox Code Playgroud)

你也可以使用MotionEvent.getActionMasked().另请参阅http://developer.android.com/reference/android/view/MotionEvent.html

  • getAction()也返回一个指针索引(你只需要处理多个触摸事件时),所以你不能安全地使用带有标志的==但必须使用掩码或调用getActionMasked(). (5认同)

Nat*_*iel 5

情侣:

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;
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

这是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>
Run Code Online (Sandbox Code Playgroud)