Android的:问题与一个按钮首要onKeyListener [英] Android: Problem with overriding onKeyListener for a Button

查看:228
本文介绍了Android的:问题与一个按钮首要onKeyListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一定的功能时,输入键pssed在Button $ P $。
当我重写安其(),我写的code为KEY_ENTER执行。这工作得很好。

I want a certain functionality when Enter key is pressed on a Button. When I override onKey(), I write the code to be executed for KEY_ENTER. This works fine.

setupButton.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (KeyEvent.KEYCODE_ENTER == keyCode)
                {
                    Intent setupIntent = new Intent(getApplicationContext(),SetUp.class);
                    startActivityForResult(setupIntent, RESULT_OK);
                }
                return true;
            }
        });

但是,没有其他按键的按键现在的工作,如向上,向下箭头键等。我知道这是因为我已经重写了安其()只ENTER键。

But no other keys work for the Button now like the Up, Down arrow keys etc. I know this is because I have overriden the onKey() for only ENTER.

但是,有没有办法,我可以保留所有其他键的功能,仅覆盖某些特定的键?

But is there a way I can retain the functionality for all other keys and override only for some specific keys?

请注意:这一切都是因为我的onClick()不被调用时,回车键为pressed。因此,我需要重写安其()本身。

Note: All this is because my onClick() is not called when Enter key is pressed. Hence, I need to override onKey() itself.

-
奇奇

- Kiki

推荐答案

当您在功能,告诉你的Andr​​oid正在处理所有的键返回true,不只是Enter键。

When you return true in the function, that tells Android you are handling all keys, not just the Enter key.

您应该只在最后返回true,if语句内,并在函数结束时返回false。

You should return true only at the end, inside the if statement and return false at the end of the function.

setupButton.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (KeyEvent.KEYCODE_ENTER == keyCode)
            {
                Intent setupIntent = new Intent(getApplicationContext(),SetUp.class);
                startActivityForResult(setupIntent, RESULT_OK);
                return true;
            }
            return false;
        }
    });

这篇关于Android的:问题与一个按钮首要onKeyListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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