Android onKeyDown() 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/974680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
onKeyDown() issue
提问by Niko Gamulin
I would like to create a photo/video capture application.
我想创建一个照片/视频捕获应用程序。
I have created a CaptureView
class which extends SurfaceView
and placed it in the main form.
我创建了一个CaptureView
扩展SurfaceView
并将其放置在主窗体中的类。
The main form's activity has onCreateOptionsMenu()
method which creates a menu. The menu worked fine but then I tried to implement a method onKeyDown
:
主窗体的活动具有onCreateOptionsMenu()
创建菜单的方法。菜单工作正常,但后来我尝试实现一种方法onKeyDown
:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN) {
switch(keyCode) {
case KeyEvent.KEYCODE_CAMERA:
videoPreview.TakePicture();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
The menu doesn't appear anymore and the method doesn't catch onKeyDown event.
菜单不再出现,并且该方法不会捕获 onKeyDown 事件。
Does anyone know what could be the reason for this issue?
有谁知道这个问题的原因是什么?
采纳答案by Niko Gamulin
I found that I was returning true
for all events, where I should only have been returning it for the code that I was using. I moved the return true inside the scope of the if
statement and returned false
otherwise That brought my menu back!
我发现我正在true
为所有事件返回,而我应该只为我正在使用的代码返回它。我将 return true 移到了if
语句的范围内,false
否则返回,这让我的菜单又回来了!
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyDown(keyCode, event);
if (keyCode == KeyEvent.KEYCODE_BACK) {
dba.close();
Intent result = new Intent("Complete");
setResult(Activity.RESULT_OK, result);
finish();
return true;
}
return false;
}
回答by Ciryon
I had a similar problem and solved it by adding
我有一个类似的问题,并通过添加解决它
this.requestFocus();
this.setFocusableInTouchMode(true);
in the constructor of my SurfaceView subclass.
在我的 SurfaceView 子类的构造函数中。
回答by rrabio
i solved removing the if statement, like this:
我解决了删除 if 语句,如下所示:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch(keyCode)
{
case KeyEvent.KEYCODE_CAMERA:
videoPreview.TakePicture();
return true;
}
return super.onKeyDown(keyCode, event);
}
回答by enam
I don't know why sometimes it does not work, though for one of my apps this keyDown()
is working fine and again when I use it for a new app then it does not work.
我不知道为什么有时它不起作用,但对于我的一个应用程序,这keyDown()
运行良好,当我将它用于新应用程序时,它又不起作用。
But I have a solution which always works:
但我有一个始终有效的解决方案:
@Override
public boolean dispatchKeyEvent (KeyEvent event) {
if (event.getAction()==KeyEvent.ACTION_DOWN && event.getKeyCode()==KeyEvent.KEYCODE_BACK) {
Toast.makeText(this, "Back button pressed", Toast.LENGTH_LONG).show();
return true;
}
return false;
}
回答by alaster
I solved this by adding into constructor this code:
我通过在构造函数中添加以下代码解决了这个问题:
setFocusable(true);
requestFocus();
thanks, Ciryon
谢谢,西里昂
Also every time I use setContentView(myView);
I must call myView.requestFocus();
. If there is a better solution please tell me.
此外,每次我使用时,setContentView(myView);
我都必须调用myView.requestFocus();
. 如果有更好的解决方案请告诉我。
回答by Dean
Well, in looking at the API documentation the only thing that stands out is that the android:clickable attribute must be set as well as the view being enabled for the onKeyDown(...) method to work.
好吧,在查看 API 文档时,唯一突出的是必须设置 android:clickable 属性以及为 onKeyDown(...) 方法启用的视图。
回答by zhujian
you can try this
你可以试试这个
this.setFocusable(true);
回答by haseman
Your Activity could be eating the key event. Override onKeyDown in the activity and throw a breakpoint in there.
您的活动可能正在吃关键事件。覆盖活动中的 onKeyDown 并在那里抛出一个断点。
Also: when you say you've "placed it in the main form" are you using the XML layout or doing in your code?
另外:当你说你“把它放在主窗体中”时,你是在使用 XML 布局还是在你的代码中做?