3 回答

TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個贊
更好的選擇是創(chuàng)建一個輕量級的Abstract類
public abstract class DoubleClickListener implements OnClickListener {
private static final long DOUBLE_CLICK_TIME_DELTA = 300;//milliseconds
long lastClickTime = 0;
@Override
public void onClick(View v) {
long clickTime = System.currentTimeMillis();
if (clickTime - lastClickTime < DOUBLE_CLICK_TIME_DELTA){
onDoubleClick(v);
lastClickTime = 0;
} else {
onSingleClick(v);
}
lastClickTime = clickTime;
}
public abstract void onSingleClick(View v);
public abstract void onDoubleClick(View v);
}
并像這樣使用
view.setOnClickListener(new DoubleClickListener() {
@Override
public void onSingleClick(View v) {
}
@Override
public void onDoubleClick(View v) {
}
});

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超8個贊
下面的代碼使用非常簡單的邏輯
boolean firstTouch = false;
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == event.ACTION_DOWN){
if(firstTouch && (Helper.getCurrentTimeInMilliSeconds() - time) <= 300) {
//do stuff here for double tap
Log.e("** DOUBLE TAP**"," second tap ");
firstTouch = false;
} else {
firstTouch = true;
time = Helper.getCurrentTimeInMilliSeconds();
Log.e("** SINGLE TAP**"," First Tap time "+time);
return false;
}
}
return true;
}
- 3 回答
- 0 關(guān)注
- 596 瀏覽
添加回答
舉報