TextView 的 onFling 不起作用

发布于 2024-11-02 20:10:02 字数 1873 浏览 1 评论 0原文

我正在尝试检测 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。

I'm trying to detect an onFling event for a TextView object. The code below works perfectly for the onDown event, however it fails to get the onScroll or onFling events. Any pointers?

Thanks

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

The layout is just a TextView inside a LinearLayout.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请你别敷衍 2024-11-09 20:10:02
    public boolean onTouch(View v, MotionEvent event)
    {
        myGestDetector.onTouchEvent(event);
        return false;
    }

我认为你应该在这里返回 true 。您视图通过返回 false 来拒绝指针向下事件,并且因此不会收到任何后续事件。

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

I think you should return true here. You view rejects pointer down event by returning false and don't receive any subsequent events because of this.

明明#如月 2024-11-09 20:10:02

但就我而言,它有效。我在 textView 的内部类中返回 true,但我确实从 TouchEvent 返回 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;
    }

But in my case it works. I return true in innerclass of textView but i do return false from my TouchEvent.

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文