Android: OnTouch, MotionEvent.ACTION_MOVE is not recognized?

自古美人都是妖i 提交于 2019-12-30 00:50:33

问题


Here is my code, I want to detect when my finger goes down the screen so when I touch the screen I detect the ACTION_DOWN but when I go down the screen with my finger, ACTION_MOVE is not recognized, neither ACTION_UP Do you know why?

        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
                    }
                }
            }
}

回答1:


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 MotionEvents in the current gesture being presented to the View.




回答2:


MotionEvent.getAction() returns more than just the flag. Try the following:

int action = arg1.getAction() & MotionEvent.ACTION_MASK;
if (action == MotionEvent.ACTION_MOVE) ...

You can also use MotionEvent.getActionMasked(). See also http://developer.android.com/reference/android/view/MotionEvent.html




回答3:


Couple of Things:

1) You need to return a boolean to show you're view consumed the event.

Here is a very simple implementation which works:

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;
            }
        });
    }
}

Here's the 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>



回答4:


There are two cases:

1) If you also set OnClickListener() -> onTouch() should return false.

(If onTouch() returns true, OnClickListener() will not work)

2) If you do not set OnClickListener() -> onTouch() should return true.

(If onTouch() returns false, only ACTION_DOWN is called)




回答5:


Check return value of onTouch() method.

it should be true, not false.

return true;

hope it will works.



来源:https://stackoverflow.com/questions/14776271/android-ontouch-motionevent-action-move-is-not-recognized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!