听 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:27:15  来源:igfitidea点击:

Listen to ENTER key in Android

androidkeylistenerenter

提问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 trueis 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 returnclause is only called afteryour Toastis 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.OnKeyListenerinterfacein your Activityclass.
  • 实现View.OnKeyListenerinterface你的Activity类。

That allows your Activityto implement the functionality provided by the interface in your class, i.e., to declareto the world that your Activityknows 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 implementskeyword in my opinion. Here the Activity"asks for a task".

请注意我说的“申报”这一事实。仅仅因为你声明你知道如何处理一个任务并不意味着人们会把这个任务交给你,也不意味着你可以自己生成这样的任务implements在我看来,这是关键字的一个很好的比喻。这里是Activity“要求任务”。

Metaphors aside, technically, the Activityis defining a way to handle that event, but it can't generate that kind of event by itself.

除了隐喻,从技术上讲,它Activity正在定义一种处理该事件的方法,但它本身无法生成那种事件。

  • setting the Viewcallbacks to your Activityimplementation
  • View回调设置为您的Activity实现

Using that, a Viewbinds to a listener (which happens to be your Activity), promising to notify it whenever the event happens.

使用它, aView绑定到一个侦听器(恰好是您的Activity),承诺在事件发生时通知它。

It "contracts" with your Activityto receive an input (the user presses the ENTER key while the Viewis in focus) and notifies the Activity. And since the Activitypreviously 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 Activityis registered by the Viewto be notified later when the Viewtrigger the event. The Activitydeclares how, but the Viewknows when.

再说一次隐喻,从技术上讲,这里Activity是由 注册的,View以便稍后在View触发事件时通知。该Activity声明如何,但View知道的时候

Conclusion:

结论:

This is just a metaphor for interfaces (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 interfaces.

这只是interfaces 的一个比喻(至少在这种情况下)。它可能看起来很复杂,但是当您将其视为两方协议时就非常清楚了。如果您需要更好的技术性解释,我建议您阅读有关interfaces.



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.

您失败了,因为您必须将侦听器设置为视图,而不仅仅是活动。