2013-12-17 20 views
1

我正在創建一個android音樂播放器。我打算讓Next和Forward按鈕在一個按鈕中工作,具體取決於按下的持續時間。在短時間內按下它作爲下一步按鈕,並在長時間按下它作爲前進按鈕。我已經嘗試過使用onLongClickListener,但是發生的是,它甚至在用戶按住按鈕之前停止轉發。我如何在動作不停止的情況下執行它,直到用戶按住按鈕?這是我的代碼。先謝謝了。結合Next和Forward功能作爲一個按鈕在Android eclipse

 btnNext.setOnLongClickListener(new OnLongClickListener() { 

     @Override 
     public boolean onLongClick(View arg0) { 
      // get current song position 
      int currentPosition = mp.getCurrentPosition(); 

      // check if seekForward time is lesser than song duration 
      if (currentPosition + seekForwardTime <= mp.getDuration()) { 
       // forward song 
       mp.seekTo(currentPosition + seekForwardTime + LONG_CLICK_LISTERNER_INTERVAL); 
      } else { 
       // forward to end position 
       mp.seekTo(mp.getDuration()); 
      } 
      return true; 

     } 
    }); 

回答

0

看到這個職位我的Events for Long click down and long click up in Android

如果要檢測觸摸事件,其採用的是Android稱爲MotionEvent,你必須覆蓋的onTouchEvent(MotionEvent五)方法,並使用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 
     } 
    } 
} 

長按下來就可以啓動一個線程在做一個循環相同,並停止在長按了,希望這可以幫助你