听ENTER键Android中 [英] Listen to ENTER key in Android

查看:116
本文介绍了听ENTER键Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的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;
    }

我不知道是怎么回事,但是当我preSS在我的键盘的回车键(我用的是Android模拟器),事件未激活

我在想什么?

推荐答案

返回不是问题。

您正在失败,因为你必须将侦听器添加到查看,而不仅仅是活动

You're failing because you must set the listener to a View, not just the Activity.

编辑澄清:

听者的返回值不旨在被理解为,该事件会或不会被称为信号。而且它不能,无论如何,因为返回子句仅称为的的你的吐司被示出。

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 after your Toast is shown.

这是一个信号,即需要采取进一步行动系统(符),或者该方法全面,妥善处理的事件(符)。这就是为什么<一href="http://developer.android.com/reference/android/view/View.OnKeyListener.html#onKey%28android.view.View,%20int,%20android.view.KeyEvent%29">documentation说这些话:

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 documentation says in these words:

返回

True如果听者的消耗的情况下,否则为false。

True if the listener has consumed the event, false otherwise.

有之间的差异:

  • 执行在活动的 View.OnKeyListener 接口 类。

这可以让你的活动来实现你的类,即所提供的接口功能,以声明的世界,你的活动知道如何处理这类事件。

That allows your Activity to implement the functionality provided by the interface in your class, i.e., to declare to the world that your Activity knows how to handle that kind of event.

请注意这样一个事实,我说的声明的。仅仅因为你宣布,你知道如何处理一个任务,并不意味着人会给这个任务给你,的,也不意味着你可以通过自己的的产生这样的任务。这是一个很好的比喻为工具关键词在我看来。在这里,活动要求任务。

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".

隐喻不谈,从技术上来说,活动是定义一个方法来处理该事件,但它本身并不能产生此类事件。

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

  • 设置查看回调到你的活动实施
  • setting the View callbacks to your Activity implementation

利用这一点,一个查看绑定到监听器(这恰好是你的活动),有前途的通知它每当事件发生。

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

据合同与你的活动来接收输入(用户presses ENTER键,而查看是焦点),并通知活动。而且,由于活动 previously宣布,它能够执行, BOTH 的各方能够履行合同为previously的同意(见previous项)。

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, BOTH parties can execute the contract as previously agreed (see previous item).

隐喻一边再次,从技术上讲,这里的活动是由查看注册来另行通知当查看触发事件。该活动声明的如何的,但查看知道的

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.

结论:

这仅仅是一个比喻接口 S(至少在这种情况下)。它可能看起来复杂,但它的清澈,当你把它看成是两方协议。如果你需要一个更好的,技术的解释,我建议阅读接口秒。

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.

答到新评论的问题:

您好大卫和其他人。我真的不能设置监听到整个活动?

Hello David and everyone else. Really I can't set a listener to the whole Activity?

不是那样的。你需要重写 dispatchKeyEvent 。举个例子:

Not in that way. You need to override dispatchKeyEvent. An example:

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

这篇关于听ENTER键Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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