首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >发布onLongClickListener安卓系统

发布onLongClickListener安卓系统
EN

Stack Overflow用户
提问于 2016-02-25 00:58:46
回答 2查看 2.6K关注 0票数 1

我有个按钮。当用户按住按钮时,我想要一个视频被记录下来。当用户释放按钮时,我想添加一些代码来处理视频并停止录制,但是当用户释放了按钮并执行onLongClickListener时,如何检测到呢?

代码语言:javascript
运行
复制
snap.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            try {
                initRecorder(mCameraView.getHolder().getSurface());
                mMediaRecorder.start();
                try {
                    Thread.sleep(10 * 1000); // This will recode for 10 seconds, if you don't want then just remove it.
                } catch (Exception e) {
                    e.printStackTrace();
                }
                finish();
                return true;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return false;
        }
    });
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-02-25 01:22:08

为了你的目的,我有一个现成的片段,看看它,https://gist.github.com/0x0af/013c4e7a90a481e04f77#file-snippet-java

基本上,您所做的是实现View.OnTouchListener()并等待MotionEvent.ACTION_DOWN和MotionEvent.ACTION_UP

更新:使用计时器确定动作是否是长按压。

票数 2
EN

Stack Overflow用户

发布于 2016-02-25 01:06:03

查看GestureDetector,在那里您可以检测到LongPress,然后分析onTouchEvent。好消息在这里Detecting a long press with Android

我使用它的方式如下:

定义GestureDetector实例:

代码语言:javascript
运行
复制
private class LongPressGestureDetector extends GestureDetector {
    private boolean longPressDetected = false;

    public LongPressGestureDetector(Context context, OnGestureListener listener) {
        super(context, listener);
    }
}

然后用它:

代码语言:javascript
运行
复制
    gestureDetector = new LongPressGestureDetector(holder.rootView.getContext(),
            new GestureDetector.SimpleOnGestureListener() {
                @Override
                public void onLongPress(MotionEvent event) {
                    gestureDetector.longPressDetected = true;
                }
            });

    //set the content touch listener
    holder.rootView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            gestureDetector.onTouchEvent(event);
            if (gestureDetector.longPressDetected) {
                Log.d(getClass().getSimpleName(), "Handle longPress touch event.");
                gestureDetector.longPressDetected = false;
                return true;
            }
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    // handle MotionEvent.ACTION_DOWN
                    break;
                }
                case MotionEvent.ACTION_UP: {
                    // handle MotionEvent.ACTION_UP
                    break;
                }
                case MotionEvent.ACTION_CANCEL: {
                    // handle MotionEvent.ACTION_CANCEL
                    break;
                }
            }
            return true;
        }
    });
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35616277

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档