Android:取消注册相机按钮 [英] Android: Unregister camera button

查看:145
本文介绍了Android:取消注册相机按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将一些动作绑定到相机按钮:

I tried to bind some actions to a camera button:

videoPreview.setOnKeyListener(new OnKeyListener(){
        public boolean onKey(View v, int keyCode, KeyEvent event){
            if(event.getAction() == KeyEvent.ACTION_DOWN)
            {
                switch(keyCode)
                {
                case KeyEvent.KEYCODE_CAMERA:
                    //videoPreview.onCapture(settings);
                    onCaptureButton();

...
                }
            }
            return false;
        }
    });

按下按钮,但应用程序崩溃,因为原始Camera应用程序启动。

Pressing the button however the application crashes because the original Camera application starts.

有人知道如何在按下相机按钮时不启动相机应用程序?

Does anyone know how to prevent Camera application start when the camera button is pressed?

推荐答案

您的示例需要返回 true ,让它知道您消费了该事件。像这样:

In your example you need to return true to let it know you "consumed" the event. Like this:

videoPreview.setOnKeyListener(new OnKeyListener(){
    public boolean onKey(View v, int keyCode, KeyEvent event){
        if(event.getAction() == KeyEvent.ACTION_DOWN) {
            switch(keyCode) {
                case KeyEvent.KEYCODE_CAMERA:
                    //videoPreview.onCapture(settings);
                    onCaptureButton();
                    /* ... */
                    return true;
            }
        }
        return false;
    }
});

只有在 videoPreview (或子元素)具有焦点。因此,您可以将其设置为默认具有焦点:

It will also only work if the videoPreview (or a child element) has focus. So you could either set it to have focus by default:

@Override
public void onResume() {
    /* ... */
    videoPreview.requestFocus();
    super.onResume();
}

或者首选将监听器放在顶层元素上。 a LinearLayout RelativeLayout 等)。

or (prefered) put the listener on the top-level element (eg. a LinearLayout, RelativeLayout, etc).

这篇关于Android:取消注册相机按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆