该GestureDetector不工作(例如,从Android开发者) [英] The GestureDetector does not work (example from android developer)

查看:501
本文介绍了该GestureDetector不工作(例如,从Android开发者)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读在Android上的姿态在Android开发人员,并按照我试着运行下面的codeS教程:

I am reading the gesture in android at android developer, and following the tutorial I tried to run the following codes:

public class MainActivity extends Activity implements OnGestureListener, OnDoubleTapListener {

    private static final String DEBUG_TAG = "Gestures";
    private GestureDetectorCompat mDetector;

    // Called when the activity is first created.
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View v = new RelativeLayout(this);
        v.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        setContentView(v);
        mDetector = new GestureDetectorCompat(this, this);
        mDetector.setOnDoubleTapListener(this);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        this.mDetector.onTouchEvent(event);
        return super.onTouchEvent(event);
    }

    @Override
    public boolean onDown(MotionEvent event) {
        Log.d(DEBUG_TAG, "onDown: " + event.toString());
        return true;
    }

    @Override
    public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
        Log.d(DEBUG_TAG, "onFling: " + event1.toString() + event2.toString());
        return true;
    }

    @Override
    public void onLongPress(MotionEvent event) {
        Log.d(DEBUG_TAG, "onLongPress: " + event.toString());
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        Log.d(DEBUG_TAG, "onScroll: " + e1.toString() + e2.toString());
        return true;
    }

    @Override
    public void onShowPress(MotionEvent event) {
        Log.d(DEBUG_TAG, "onShowPress: " + event.toString());
    }

    @Override
    public boolean onSingleTapUp(MotionEvent event) {
        Log.d(DEBUG_TAG, "onSingleTapUp: " + event.toString());
        return true;
    }

    @Override
    public boolean onDoubleTap(MotionEvent event) {
        Log.d(DEBUG_TAG, "onDoubleTap: " + event.toString());
        return true;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent event) {
        Log.d(DEBUG_TAG, "onDoubleTapEvent: " + event.toString());
        return true;
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent event) {
        Log.d(DEBUG_TAG, "onSingleTapConfirmed: " + event.toString());
        return true;
    }
}

在codeS来自网站这里

但是,当我跑的应用程序,我没有调试信息,当我点击或长preSS的看法。

But when I ran the app, I got no debug information when I click or long press the view.

顺便说一句,我测试的两个模拟器和HTC E1设备中的应用程序。

BTW, I test the app in both emulator and htc e1 device.

这是什么问题?

推荐答案

您正在创建一个GestureDetector,但你永远不会挂钩起来你查看。 试着改变你的onCreate是这样的:

You are creating a GestureDetector but you are never "hooking it up" to your View. Try changing your onCreate like this:

super.onCreate(savedInstanceState);
View v = new RelativeLayout(this);
v.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
setContentView(v);
mDetector = new GestureDetectorCompat(this, this);
mDetector.setOnDoubleTapListener(this);
v.setOnTouchListener(new OnTouchListener(){
    public boolean onTouch(View v, MotionEvent me){
        return mDetector.onTouchEvent(me);
    }
});

这篇关于该GestureDetector不工作(例如,从Android开发者)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆