0
要求:確保itemLookup
僅在按鈕被保持至少1秒後才執行。當按鈕被保持3秒時停止按鈕事件,因此用於查找的記錄不會包含不必要的數據。Android:觸發MotionEvent.ACTION_CANCEL
- 問題:
MotionEvent.ACTION_CANCEL
即使調試確認TableLayoutForIntercept
的onInterceptTouchEvent
被調用之前MainActivity
「稱爲sOnTouchListener
事件,預期不會被調用。也許我不明白onInterceptTouchEvent
的目的?我已看過有關此事的其他帖子,但所有人都在處理刷卡或拖動事件,而不是取消按鈕按。也許這不可行? 代碼:僅顯示
MainActivity
的相關部分,以及完整的TableLayoutForIntercept
類,當然還有<com.company.myapplication.TableLayoutForIntercept></com.company.myapplication.TableLayoutForIntercept>
標記環繞着我的xml佈局。public class MainActivity extends Activity { //... DateTime recordingStartedTime; DateTime recordingEndedTime; boolean buttonHeldLongEnough = false; PackageManager pm = getPackageManager(); boolean micPresent = pm.hasSystemFeature(PackageManager.FEATURE_MICROPHONE); if (micPresent) { recordBtn.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View recordView, MotionEvent recordEvent) { switch (recordEvent.getAction()) { case MotionEvent.ACTION_DOWN: // Try to record audio try { recordingOff.setVisibility(View.INVISIBLE); recordingOn.setVisibility(View.VISIBLE); recordingStartedTime = DateTime.now(); constructPrepareStartRecording(); } catch (Exception ex) { Log.e(MainActivity.class.getSimpleName(), "An unknown error occurred."); } return true; case MotionEvent.ACTION_UP: recordingOff.setVisibility(View.VISIBLE); recordingOn.setVisibility(View.INVISIBLE); recordingEndedTime = DateTime.now(); Seconds seconds = Seconds.secondsBetween(recordingStartedTime, recordingEndedTime); int secondsButtonHeld = seconds.getSeconds(); // Button must have been held at least 1 second before running itemLookup if (secondsButtonHeld > 0) { buttonHeldLongEnough = true; } else { buttonHeldLongEnough = false; } // Need to release resources regardless stopReleaseResetRecording(); if (buttonHeldLongEnough) { itemLookup(); } return true; case MotionEvent.ACTION_CANCEL: // I think this is the event I have to trigger to halt the button press boolean codeHasHitCancel = true; return codeHasHitCancel; } return false; } }); } else { toastTitle = "Unable To Record"; toastMessage = "Device microphone not found."; toast = new GenericCustomToast(); toast.show(toastTitle, toastMessage, MainActivity.this); } //... } public class TableLayoutForIntercept extends TableLayout { public TableLayoutForIntercept (Context context) { super(context); } public TableLayoutForIntercept (Context context, AttributeSet attrs) { super(context, attrs); } private CancelPressTask cancelPressTask = null; private boolean stopTouchEvent = false; @Override public boolean onInterceptTouchEvent (MotionEvent event) { final int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: stopTouchEvent = false; cancelPressTask = new CancelPressTask(); break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: cancelPressTask.resetCancelPressTimer(); cancelPressTask.stopCancelPressTimer(); return stopTouchEvent; } return super.onInterceptTouchEvent(event); } @Override public boolean onTouchEvent (MotionEvent event) { if (!stopTouchEvent) { return super.onTouchEvent(event); } return true; } private class CancelPressTask { public final long CANCEL_PRESS_TIMEOUT = 3000; // 3 seconds private Handler cancelPressHandler = new Handler(){ public void handleMessage(Message msg) { } }; private Runnable cancelPressCallback = new Runnable() { @Override public void run() { stopTouchEvent = true; } }; public void resetCancelPressTimer(){ cancelPressHandler.removeCallbacks(cancelPressCallback); cancelPressHandler.postDelayed(cancelPressCallback, CANCEL_PRESS_TIMEOUT); } public void stopCancelPressTimer(){ cancelPressHandler.removeCallbacks(cancelPressCallback); } } }