龙preSS的Andr​​oid [英] Long press Android

查看:90
本文介绍了龙preSS的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题,在我的自定义视图检测长preSS。

I have problems with detecting long press in my custom view.

这里的code与此相关的问题

Here's the code related to this issue

final GestureDetector gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
    public void onLongPress(MotionEvent e) {
        Log.e("dbg_msg", "onLongPress");
    }
});

public boolean onTouchEvent(MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
};

这code检测每一个(短)点击,只要preSS。

This code detects every single (short) click as long press.

当我把这个code类的活动继承,它的工作原理。

When I put this code in class inherited from Activity, it works.

那么,为什么它不工作在自定义视图?

So why it isn't working in custom View ?

推荐答案

这一切code。在您的自定义视图类中有云:

All of this code goes in your custom view class:

public static int LONG_PRESS_TIME = 500; // Time in miliseconds 

final Handler _handler = new Handler(); 
Runnable _longPressed = new Runnable() { 
    public void run() {
        Log.i("info","LongPress");
    }   
};

@Override
public boolean onTouchEvent(MotionEvent event) {
    switch(event.getAction()){
    case MotionEvent.ACTION_DOWN:
        _handler.postDelayed(_longPressed, LONG_PRESS_TIME);
        break;
    case MotionEvent.ACTION_MOVE:
        _handler.removeCallbacks(_longPressed);
        break;
    case MotionEvent.ACTION_UP:
        _handler.removeCallbacks(_longPressed);
        break;
    }
    return true;
}

这篇关于龙preSS的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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