android中的DoubleTap

我需要创建一个小的文本区域。当我双击该文本区域时,它将移至下一个活动。我该怎么做?



慕斯709654
浏览 560回答 3
3回答

海绵宝宝撒

更好的选择是创建一个轻量级的Abstract类public abstract class DoubleClickListener implements OnClickListener {&nbsp; &nbsp; private static final long DOUBLE_CLICK_TIME_DELTA = 300;//milliseconds&nbsp; &nbsp; long lastClickTime = 0;&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; long clickTime = System.currentTimeMillis();&nbsp; &nbsp; &nbsp; &nbsp; if (clickTime - lastClickTime < DOUBLE_CLICK_TIME_DELTA){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onDoubleClick(v);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lastClickTime = 0;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onSingleClick(v);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; lastClickTime = clickTime;&nbsp; &nbsp; }&nbsp; &nbsp; public abstract void onSingleClick(View v);&nbsp; &nbsp; public abstract void onDoubleClick(View v);}并像这样使用&nbsp;view.setOnClickListener(new DoubleClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onSingleClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onDoubleClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });

ITMISS

下面的代码使用非常简单的逻辑&nbsp; &nbsp; boolean firstTouch = false;&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public boolean onTouchEvent(MotionEvent event) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(event.getAction() == event.ACTION_DOWN){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(firstTouch && (Helper.getCurrentTimeInMilliSeconds() - time) <= 300) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //do stuff here for double tap&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e("** DOUBLE TAP**"," second tap ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; firstTouch = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; firstTouch = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time = Helper.getCurrentTimeInMilliSeconds();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e("** SINGLE&nbsp; TAP**"," First Tap time&nbsp; "+time);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android