听 Android 中的 ENTER 键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11237731/
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
Listen to ENTER key in Android
提问by Carlos Pereira
Here is my code:
这是我的代码:
public class CaptureENTER extends Activity implements OnKeyListener{
/* on create and other stuff in here*/
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
Toast.makeText(LiVoiceActivity.this,
"YOU CLICKED ENTER KEY",
Toast.LENGTH_LONG).show();
}
return false;
}
I don't know what is going on, but when I press the ENTER key in my keyboard (I am using the Android emulator), the event is not activated.
我不知道发生了什么,但是当我按下键盘上的 ENTER 键时(我使用的是 Android 模拟器),事件未激活。
What am I missing?
我错过了什么?
回答by davidcesarino
Returning true
is not the issue.
返回true
不是问题。
You're failing because you must set the listener to a View
, not just the Activity
.
您失败了,因为您必须将侦听器设置为 a View
,而不仅仅是Activity
.
edited for clarification:
编辑以澄清:
The return value of the listener is not meant to be understood as a signal that the event will or will not be called. And it couldn't anyway, since the return
clause is only called afteryour Toast
is shown.
监听器的返回值并不意味着被理解为事件将被调用或不会被调用的信号。无论如何它都不能,因为该return
子句仅在您Toast
显示后才被调用。
It's a signal to the system that further action is needed (return false
) or that the method handled the event fully and properly (return true
). That's why the documentationsays in these words:
这是向系统发出需要采取进一步行动(返回false
)或方法完全正确地处理事件(返回true
)的信号。这就是为什么文档用这些话说:
Returns
True if the listener has consumedthe event, false otherwise.
退货
如果侦听器已经消费了事件,则为真,否则为假。
There's a difference between:
有以下区别:
- Implementing the
View.OnKeyListener
interface
in yourActivity
class.
- 实现
View.OnKeyListener
interface
你的Activity
类。
That allows your Activity
to implement the functionality provided by the interface in your class, i.e., to declareto the world that your Activity
knows how to handle that kind of event.
这允许您Activity
在类中实现接口提供的功能,即向全世界声明您Activity
知道如何处理这种事件。
Please pay attention to the fact that I said "declare". Just because you declared that you know how to handle a task doesn't mean people will give that task to you, nor does it mean that you can generate such tasks by yourself. It's a good metaphor for the implements
keyword in my opinion. Here the Activity
"asks for a task".
请注意我说的“申报”这一事实。仅仅因为你声明你知道如何处理一个任务并不意味着人们会把这个任务交给你,也不意味着你可以自己生成这样的任务。implements
在我看来,这是关键字的一个很好的比喻。这里是Activity
“要求任务”。
Metaphors aside, technically, the Activity
is defining a way to handle that event, but it can't generate that kind of event by itself.
除了隐喻,从技术上讲,它Activity
正在定义一种处理该事件的方法,但它本身无法生成那种事件。
- setting the
View
callbacks to yourActivity
implementation
- 将
View
回调设置为您的Activity
实现
Using that, a View
binds to a listener (which happens to be your Activity
), promising to notify it whenever the event happens.
使用它, aView
绑定到一个侦听器(恰好是您的Activity
),承诺在事件发生时通知它。
It "contracts" with your Activity
to receive an input (the user presses the ENTER key while the View
is in focus) and notifies the Activity
. And since the Activity
previously declared that it's capable of performing that, BOTHparties can execute the contract as previously agreed(see previous item).
它与您“签约”Activity
以接收输入(用户View
在聚焦时按下 ENTER 键)并通知Activity
. 而且,由于Activity
先前宣布,它能够执行的,BOTH如先前商定各方能够履行合同(参见上文)。
Metaphors aside again, technically, here the Activity
is registered by the View
to be notified later when the View
trigger the event. The Activity
declares how, but the View
knows when.
再说一次隐喻,从技术上讲,这里Activity
是由 注册的,View
以便稍后在View
触发事件时通知。该Activity
声明如何,但View
知道的时候。
Conclusion:
结论:
This is just a metaphor for interface
s (at least in this case). It may look complicated, but it's crystal clear when you think of it as a two-party agreement. If you need a better, technical, explanation, I suggest reading about interface
s.
这只是interface
s 的一个比喻(至少在这种情况下)。它可能看起来很复杂,但是当您将其视为两方协议时就非常清楚了。如果您需要更好的技术性解释,我建议您阅读有关interface
s.
Answer to the new comment question:
回答新评论问题:
Hello David and everyone else. Really I can't set a listener to the whole Activity?
你好大卫和其他人。我真的不能为整个 Activity 设置一个监听器吗?
Not in that way. You need to override dispatchKeyEvent
. An example:
不是那样。您需要覆盖dispatchKeyEvent
. 一个例子:
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
Toast.makeText(UITestsActivity.this,
"YOU CLICKED ENTER KEY",
Toast.LENGTH_LONG).show();
return true;
}
return super.dispatchKeyEvent(e);
};
回答by Yaqub Ahmad
Try this:
尝试这个:
public class CaptureENTER extends Activity implements OnKeyListener{
/* on create and other stuff in here*/
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
Toast.makeText(LiVoiceActivity.this,
"YOU CLICKED ENTER KEY",
Toast.LENGTH_LONG).show();
return true;
}
return false;
}
EDIT:Davidis CORRECT!!
编辑:大卫是正确的!!
Returning true is not the issue.
返回 true 不是问题。
You're failing because you must set the listener to a View, not just the Activity.
您失败了,因为您必须将侦听器设置为视图,而不仅仅是活动。