Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:触发MotionEvent.ACTION\u取消_Java_Android_Button_Touch Event_Motionevent - Fatal编程技术网

Java Android:触发MotionEvent.ACTION\u取消

Java Android:触发MotionEvent.ACTION\u取消,java,android,button,touch-event,motionevent,Java,Android,Button,Touch Event,Motionevent,要求:确保仅在按住按钮至少1秒的情况下执行itemLookup。按住按钮3秒后停止按钮事件,以便用于查找的记录不会包含不必要的数据 问题是:MotionEvent.ACTION\u CANCEL从未被调用,即使调试确认在调用MainActivity的OnTouchListener事件之前调用了TableLayoutForIntercept。也许我不明白InterceptTouchEvent的作用是什么?我看过其他关于这件事的帖子,但它们都是处理滑动或拖动事件,而不是取消按键。也许这不能做到 代

要求:确保仅在按住按钮至少1秒的情况下执行
itemLookup
。按住按钮3秒后停止按钮事件,以便用于查找的记录不会包含不必要的数据

  • 问题是:
    MotionEvent.ACTION\u CANCEL
    从未被调用,即使调试确认在调用
    MainActivity
    OnTouchListener
    事件之前调用了
    TableLayoutForIntercept
    。也许我不明白InterceptTouchEvent的作用是什么?我看过其他关于这件事的帖子,但它们都是处理滑动或拖动事件,而不是取消按键。也许这不能做到
  • 代码:只显示了main活动的相关部分,以及完整的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);
            }
        }
    }
    

我想出了另一种方法。我没有试图触发事件,而是决定偷它!下面的代码将停止录制,然后在3秒钟后进行项目查找,无论用户尝试按住按钮多长时间。唯一的问题是录制的时间太短(少于1秒),通过toast消息通知用户仍然需要整整3秒钟。但我愿意接受这一点

    public class Main Activity extends Activity {

        DateTime recordingStartedTime;
        DateTime recordingEndedTime = null;
        boolean buttonHeldLongEnough = false;
        LimitRecordingTask limitRecordingTask;

        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) {

                    limitRecordingTask = new LimitRecordingTask();
                    switch (recordEvent.getAction()) {

                        case MotionEvent.ACTION_DOWN:
                            // Try to record audio
                            try {
                                recordBtn.setBackgroundColor(Color.DKGRAY);
                                recordingOff.setVisibility(View.INVISIBLE);
                                recordingOn.setVisibility(View.VISIBLE);

                                recordingStartedTime = DateTime.now();
                                constructPrepareStartRecording();
                                limitRecordingTask.resetRecordingTimer();
                            }
                            catch (Exception ex) {
                                Log.e(MainActivity.class.getSimpleName(), "An unknown error occurred.");
                                limitRecordingTask.stopRecordingTimer();
                            }
                            return true;

                        case MotionEvent.ACTION_UP:
                            // After 3 seconds limitRecordingTask will 'steal' this event
                            recordBtn.setBackgroundResource(R.drawable.custom_button);
                            recordingOff.setVisibility(View.VISIBLE);
                            recordingOn.setVisibility(View.INVISIBLE);

                            recordingEndedTime = DateTime.now();
                            Seconds seconds = Seconds.secondsBetween(recordingStartedTime, recordingEndedTime);
                            int secondsButtonHeld = Math.abs(seconds.getSeconds());

                            if (secondsButtonHeld > 0 ) {
                                buttonHeldLongEnough = true;
                            }
                            else {
                                buttonHeldLongEnough = false;
                            }
                            return true;
                    }
                    return false;
                }
            });
        }
        else {
            toastTitle = "Unable To Record";
            toastMessage = "Device microphone not found.";
            toast = new GenericCustomToast();
            toast.show(toastTitle, toastMessage, MainActivity.this);
        }

        private class LimitRecordingTask {

            public final long RECORDING_TIMEOUT = 3000; // 3 seconds

            private Handler limitRecordingHandler = new Handler(){
                public void handleMessage(Message msg) {
                }
            };

            private Runnable limitRecordingCallback = new Runnable() {
                @Override
                public void run() {
                    // 'Stolen' MotionEvent.ACTION_UP
                    recordBtn.setBackgroundResource(R.drawable.custom_button);
                    recordingOff.setVisibility(View.VISIBLE);
                    recordingOn.setVisibility(View.INVISIBLE);

                    if (recordingEndedTime == null) {
                        recordingEndedTime = DateTime.now();
                    }
                    Seconds seconds = Seconds.secondsBetween(recordingStartedTime, recordingEndedTime);
                    int secondsButtonHeld = Math.abs(seconds.getSeconds());

                    if (secondsButtonHeld > 0 ) {
                        buttonHeldLongEnough = true;
                    }
                    else {
                        buttonHeldLongEnough = false;
                    }

                    limitRecordingTask.stopRecordingTimer();

                    stopReleaseResetRecording();
                    itemLookup();
                }
            };

            public void resetRecordingTimer(){
                limitRecordingHandler.removeCallbacks(limitRecordingCallback);
                limitRecordingHandler.postDelayed(limitRecordingCallback, RECORDING_TIMEOUT);
            }

            public void stopRecordingTimer(){
                limitRecordingHandler.removeCallbacks(limitRecordingCallback);
            }
        }
    }