요구 사항 : 버튼을 1 초 이상 누르고있는 경우에만 itemLookup
이 수행되어야합니다. 단추를 3 초 동안 누르고 있으면 조회에 사용 된 녹음에 불필요한 데이터가 포함되지 않도록 단추 이벤트를 중지하십시오.Android : 트리거링 MotionEvent.ACTION_CANCEL
- 문제점 : 디버깅의
OnTouchListener
이벤트라고 예상대로TableLayoutForIntercept
, 'MainActivity
가 이전의onInterceptTouchEvent
가 호출되는'것을 확인하더라도MotionEvent.ACTION_CANCEL
가 호출되지 않습니다. 아마도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); } } }