2013-02-14 103 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