对 Android 按键事件处理感到困惑。有什么好的解释吗?

发布于 2024-09-19 22:13:24 字数 292 浏览 12 评论 0原文

我是 Android 的新手。对于如何在 EditText/TextView 中监听按键和软键,有人有一个理智的解释吗?

我很想看到一个全面的教程或一组示例。

据我了解,我可以向我的 Activity 添加一个 KeyListener,例如 onKeyDown()、onKeyUp(),但是当我尝试这样做时,我无法触发仅 HOME 和 BACK 等普通键的事件。

我见过提到使用 TextWatcher,但这与处理原始按键事件不同。

SO 上似乎有很多半解决方案。希望能帮您解开迷雾……

I'm a relative beginner with Android. Does anybody have a sane explanation for how to listen for keys and soft keys in an EditText/TextView?

I'd love to see a comprehensive tutorial or set of examples.

As I understand it, I can add a KeyListener to my Activity, e.g. onKeyDown(), onKeyUp() but when I try this I can't trigger the events for normal keys only HOME and BACK for example.

I have seen mention of using a TextWatcher but that isn't the same as handling raw key events.

There seem to be a number of half-solutions here on SO. Hoping you can help clear the mists of confusion...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

思念绕指尖 2024-09-26 22:13:24

您必须将一个关键侦听器分配给 EditText 本身,而不是 Activity。

You have to assign a key listener not to activity but rather to EditText itself.

拧巴小姐 2024-09-26 22:13:24

这就是我必须监听 BACK 或 MENU 键事件。只需添加此方法,无需实现任何接口。我在我的 BaseActivity 中执行此操作,每个 Activity 都从中继承。

public boolean onKeyDown(int keyCode, KeyEvent event) {
    Log.d(NAME, "Key pressed");

    switch (keyCode) {
    case KeyEvent.KEYCODE_BACK:
        Log.d(NAME, "Back pressed");
        // IGNORE back key!!
        return true;
        /* Muestra el Menú de Opciones */
    case KeyEvent.KEYCODE_MENU:
        Intent menu = new Intent(this, Menu.class);

        // start activity
        startActivity(menu);
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

PS:我强烈建议不要忽略后退键。

This is what I have to listen to BACK or MENU key events. Simply add this method, without implementing any Interface. I do this in my BaseActivity, from which every Activity inherits.

public boolean onKeyDown(int keyCode, KeyEvent event) {
    Log.d(NAME, "Key pressed");

    switch (keyCode) {
    case KeyEvent.KEYCODE_BACK:
        Log.d(NAME, "Back pressed");
        // IGNORE back key!!
        return true;
        /* Muestra el Menú de Opciones */
    case KeyEvent.KEYCODE_MENU:
        Intent menu = new Intent(this, Menu.class);

        // start activity
        startActivity(menu);
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

PS: I highly discourage ignoring the back key.

魔法唧唧 2024-09-26 22:13:24

例如:

myEditText.setOnKeyListener(new OnKeyListener() {
     public boolean onKey(View v, int keyCode, KeyEvent event) {
         if (event.getAction() == KeyEvent.ACTION_DOWN)
             if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER){
                //your code here
             }
         return false;
     }
});

For example:

myEditText.setOnKeyListener(new OnKeyListener() {
     public boolean onKey(View v, int keyCode, KeyEvent event) {
         if (event.getAction() == KeyEvent.ACTION_DOWN)
             if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER){
                //your code here
             }
         return false;
     }
});
寂寞陪衬 2024-09-26 22:13:24

我最近发现了另一种方法,即使用 Activity onKeyDown 或使用 view.setOnKeyListener< 在视图上设置关键侦听器的事件(在我的情况下,这并不能真正处理来自 ADB 的关键事件)。 /代码>。

由于 android P 方法 addOnUnhandledKeyEventListener 已经被引入。当您的视图能够捕获未处理的关键事件时,它允许您做任何您需要做的事情。

这是我如何使用它的示例:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) yourView.addOnUnhandledKeyEventListener { v, event ->
    when (event.keyCode) {
        KeyEvent.KEYCODE_UNKNOWN -> {
            TODO("Do whatever you need to do.")
            true // Specify you did handle the event
        }
        KeyEvent.KEYCODE_SOFT_RIGHT -> {
            TODO("Do whatever you need to do.")
            true // Specify you did handle the event
        }
        // etc...
        else -> false // Specify you didn't handle the event
    }
}

I recently found another way that be stuck using Activity onKeyDown, or event setting a key listener on view (which is not really working with key events from ADB in my case) with view.setOnKeyListener.

Since android P method addOnUnhandledKeyEventListener has been introduced. It allows you to do whatever you need to do when your view is able to catch unhandled key events.

Here is an example of how I used it :

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) yourView.addOnUnhandledKeyEventListener { v, event ->
    when (event.keyCode) {
        KeyEvent.KEYCODE_UNKNOWN -> {
            TODO("Do whatever you need to do.")
            true // Specify you did handle the event
        }
        KeyEvent.KEYCODE_SOFT_RIGHT -> {
            TODO("Do whatever you need to do.")
            true // Specify you did handle the event
        }
        // etc...
        else -> false // Specify you didn't handle the event
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文