安卓:注销照相机按钮照相机、按钮

2023-09-05 00:06:25 作者:再贱就再见"

我想结合一些行动,以一个拍照键:

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;
		}
	});

pressing按钮但是应用程序崩溃,因为原来的相机应用程序启动。

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

有谁知道如何prevent相机应用程序启动时,相机按钮是pressed?

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

推荐答案

在您的例子中,你需要回到来让它知道你消费的事件。像这样的:

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;
    }
});

这也只会工作,如果视频preVIEW (或子元素)具有焦点。所以,你可以将它设置为具有焦点默认为:

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();
}

或(prefered)把听者的顶级元素(例如,一个的LinearLayout RelativeLayout的等)。