onFling for TextView无法正常工作

时间:2011-04-22 06:56:30

标签: android textview gesturedetector onfling

我正在尝试检测TextView对象的onFling事件。下面的代码适用于onDown事件,但它无法获取onScroll或onFling事件。有什么指针吗?

由于

package rob.testapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity
{
    private GestureDetector myGestDetector;
    private TextView mainTextView;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        myGestDetector = new GestureDetector(this, new SimpleOnGestureListener()
        {
            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
            {
                mainTextView.setText("just scroll'n");
                return false;
            }

            @Override
            public boolean onDown(MotionEvent e1)
            {
                mainTextView.setText("on down");
                return false;
            }

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
            {
                mainTextView.setText("on fling");
                return false;
            }
        });

        mainTextView = (TextView)findViewById(R.id.MainText);
        mainTextView.setText("Starting app...");
        mainTextView.setOnTouchListener(new View.OnTouchListener()
        {
            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                myGestDetector.onTouchEvent(event);
                return false;
            }
        });
    }
}

布局只是LinearLayout中的TextView。

2 个答案:

答案 0 :(得分:3)

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

我认为你应该在这里回归真实。您可以通过返回false来查看拒绝指针向下事件,并且不会因此而接收任何后续事件。

答案 1 :(得分:0)

但在我的情况下,它有效。我在textView的内部类中返回true但我确实从我的Touch事件中返回false。

public class FingureGesture extends Activity implements OnGestureListener,OnDoubleTapListener{
    GestureDetector detector;
    File sdcard = Environment.getExternalStorageDirectory();
    TextView textView;


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

        detector = new GestureDetector(this,this);
        textView = (TextView) findViewById(R.id.editText1);
        textView.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                Log.i("OnTouch", "Im here ");
                    detector.onTouchEvent(event);
                return false;
            }
        });     
    }//end of onCreate

    public boolean onDown(MotionEvent e) {
        Log.i("onDown ", "x:: "+e.getX()+"\ny:: "+e.getY());
        return false;
    }

    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {  
        return false;
    }


    public boolean onTouchEvent(MotionEvent me){ 

        Log.i(" onTouchEvent", me.toString());
        this.detector.onTouchEvent(me);
//      return super.onTouchEvent(me);
        return true;
    }
    public void onLongPress(MotionEvent e) {
        Log.i(" onLongPress", e.toString());

    }

    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
        Log.i("On Scroll ","\n#e1 :: "+ e1.getX()+"\n#Histo e1.x"+"\n#e2 :: "+e2.getX());
        return false;
    }

    public void onShowPress(MotionEvent e) {
        Log.i(" onShowPress", e.toString());

    }

    public boolean onSingleTapUp(MotionEvent e) {
        Log.i("onSingleTapUp ", e.toString());
        return false;
    }

    public boolean onDoubleTap(MotionEvent e) {
        Log.i(" onDoubleTap", e.toString());
        return false;
    }

    public boolean onDoubleTapEvent(MotionEvent e) {
        Log.i(" onDoubleTapEvent", e.toString());
        return false;
    }

    public boolean onSingleTapConfirmed(MotionEvent e) {
        Log.i(" onSingleTapConfirmed", e.toString());
        return false;
    }