2013-07-27 136 views

回答

14

我终于实现了一个触摸屏长按代表长击事件的TAP事件序列的开始时,THX所有:

textView.setOnTouchListener(new View.OnTouchListener() { 

    private static final int MIN_CLICK_DURATION = 1000; 
    private long startClickTime; 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 

     switch (event.getAction()) { 
     case MotionEvent.ACTION_UP: 
      longClickActive = false; 
      break; 
     case MotionEvent.ACTION_DOWN: 
      if (longClickActive == false) { 
       longClickActive = true; 
       startClickTime = Calendar.getInstance().getTimeInMillis(); 
      } 
      break; 
     case MotionEvent.ACTION_MOVE: 
      if (longClickActive == true) { 
       long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime; 
       if (clickDuration >= MIN_CLICK_DURATION) { 
        Toast.makeText(MainActivity.this, "LONG PRESSED!",Toast.LENGTH_SHORT).show(); 
        longClickActive = false; 
       } 
      } 
      break; 
     } 
     return true; 
    } 
}); 

其中private boolean longClickActive = false;是一个类变量。

+2

感谢这导致我的解决方案。 – ctapp1

+0

问题是长时间轻敲等待ACTION_MOVE,有时不会调用,但必须捕获长时间轻敲 –

2

为了计算触摸算你能得到您的活动getPointerCount()之类here

和长按也许this帮助

编辑:希望this link帮助您确定获得触摸持续时间

+0

我需要时间不算老兄。 –

+1

也许我无法理解你,所以希望编辑答案和附加链接帮助你更多,祝你好运 –

+0

这不完全是我的答案,但启发我通过使用布尔标志。 thx:D –

1

您必须在ACTION_DOWN和ACTION_UP事件之间计算时间。 这是不可能只在ACTOIN_DOWN状态来计算这个时间,因为它是

+0

Thx,我通过使用ACTION_DOWN和ACTION_MOVE来完成。 –

1

试试这个。你不需要为此找到破解。

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

public boolean onTouchEvent(MotionEvent event) { 
if (gestureDetector.onTouchEvent(event)) { 
    return true; 
} 
switch (event.getAction()) { 
    case MotionEvent.ACTION_UP: 
    break; 
    case MotionEvent.ACTION_DOWN: 
    break; 
    case MotionEvent.ACTION_MOVE: 
    break; 
} 
return true; 
} 
};