2013-02-14 144 views
2

我想要两个单独的事件用于长按一下,然后长点击。我如何在Android中执行此操作?长时间点击并长时间点击Android的事件

我已经试过如下

public class FfwRewButton extends ImageButton { 

    public interface ButtonListener { 

     void OnLongClickDown(View v); 

     void OnLongClickUp(View v); 
    } 

    private ButtonListener mListener; 

    private boolean mLongClicked = false; 

    public FfwRewButton(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     setFocusable(true); 
     setLongClickable(true); 
    } 

    public FfwRewButton(Context context) { 
     this(context, null); 
    } 

    public FfwRewButton(Context context, AttributeSet attrs) { 
     this(context, attrs, android.R.attr.imageButtonStyle); 
    } 

    @Override 
    public boolean onKeyLongPress(int keyCode, KeyEvent event) { 
     Log.d("my listener", "long press"); 
     mLongClicked = true; 
     mListener.OnLongClickDown(this); 
     return super.onKeyLongPress(keyCode, event); 
    } 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     Log.d("my listener", "key down"); 
     mLongClicked = false; 
     if (true) { 
      super.onKeyDown(keyCode, event); 
      return true; 
     } 
     return super.onKeyDown(keyCode, event); 
    } 

    @Override 
    public boolean onKeyUp(int keyCode, KeyEvent event) { 
     Log.d("my listener", "key up"); 
     if (mLongClicked) 
      mListener.OnLongClickUp(this); 
     return super.onKeyUp(keyCode, event); 
    } 

    public void setFfwRewButtonListener(ButtonListener listener) { 
     mListener = listener; 
    } 
} 

,并在活动中我把它叫做这样

private FfwRewButton.ButtonListener mListener = new FfwRewButton.ButtonListener() { 

     @Override 
     public void OnLongClickUp(View v) { 
      Log.d(TAG, "longClickup"); 
     } 

     @Override 
     public void OnLongClickDown(View v) { 
      Log.d(TAG, "longClickdown"); 
     } 
    }; 

但仍然不是在logcat的 获得任何日志消息的任何人都可以帮助我;我错在哪里?

+0

by onLongClickUp你是指当用户在longClicking后抬起手指时? – FoamyGuy 2013-02-14 14:33:52

+0

是的,当用户抬起手指 – 2013-02-14 14:36:19

+1

使用触摸事件而不是按键事件 – Rajesh 2013-02-14 14:37:34

回答

6

实现onKeyXXX()方法适用于键盘或按键的关键事件,如菜单键,搜索键等。

如果要检测Android中的触摸事件(称为MotionEvent),则必须覆盖onTouchEvent(MotionEvent e)方法并使用GestureDetector类识别长按。

private GestureDetector mGestureDetector; 

public FfwRewButton(...) { 
    //.... 
    mGestureDetector = new GestureDetector(context, 
     new GestureDetector.SimpleOnGestureListener() { 
      public boolean onDown(MotionEvent e) { 
       mLongClicked = false; 
       return true; 
      } 
      public void onLongPress(MotionEvent e) { 
       mLongClicked = true; 
       // long press down detected 
      } 
     }); 
    } 

    public boolean onTouchEvent(MotionEvent e) { 
     mGestureDetector.onTouchEvent(e); 
     if (mLongClicked && e.getAction() == ACTION_UP) { 
      // long press up detected 
     } 
    } 
} 
+0

这样的事情是可能比我提出的要好,因为它使用真正的听众来获取正常的长时间点击,而不是像我那样手动跟踪时间。 – FoamyGuy 2013-02-14 14:47:59

+0

如何将我的按钮连接到这个gesturedetector? – 2013-02-14 15:21:56

+0

它已连接。如果您查看FfwRewButton类中的onTouchEvent()方法,它会调用GestureDetector对象的onTouchEvent()方法。 – 2013-02-14 15:31:10

3

像这样的事情将让你在正确的道路上,

我没有编译,所以你可能需要纠正一些语法的东西,但你的目标可以用这个概念

OnTouchListener mTouchListener = new OnTouchListener(){ 
    private totalTimeDown = -1; 
    private downTime = -1; 
    public boolean onTouch(View v, MotionEvent me){ 
    if(me.getAction() == MotionEvent.ACTION_DOWN){ 
     downTime = System.getCurrentTimeInMillis(); 
     return true; 
    } 

    if(me.getAction() == MotionEvent.ACTION_UP){ 
     totalTimeDown = System.getCurrentTimeInMillis() - downTime; 
     if(totalTimeDown > 500){ 
      //Finger was down long enough for "longClick" 
      return true; 
     } 
    } 
    return false; 
    } 
}); 
+0

好吧,我会尽力让你回来:)谢谢 – 2013-02-14 14:44:45

相关问题