为什么 if else 语句的两边都会被执行,以及如何解决?

发布于 2024-12-03 13:53:15 字数 640 浏览 3 评论 0原文

我有一段代码:

passwordEditText.setOnKeyListener(new OnKeyListener() 
    {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) 
        {
            if (keyCode == KeyEvent.KEYCODE_ENTER)
            {
                launch.performClick();
                return true;

            }
            else
            {
                return false;
            }
        }
    });

我想要的是,当按下回车键时,它会执行登录命令(启动是执行登录的按钮)。但是,执行 true 块后,它还会继续执行 else 块,返回 false 并导致(仅在某些设备上)第二次登录。

所以我的问题分为两部分:if else 语句如何评估为 true 和 false,以及我怎样才能使它不这样做。我想到了一些技巧来实现这一点,但这似乎是一个可以更好地理解然后快速修复的问题。

I have a block of code:

passwordEditText.setOnKeyListener(new OnKeyListener() 
    {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) 
        {
            if (keyCode == KeyEvent.KEYCODE_ENTER)
            {
                launch.performClick();
                return true;

            }
            else
            {
                return false;
            }
        }
    });

What I want is that when the enter key is pressed it performs the log in command (launch is the button that executes the log in). However, after executing the true block, it continues on to execute the else block as well, returning false and causing (only on some devices) the log in to occur a second time.

So my question is in two parts: How can a if else statement evaluate as both true and false, and how can I make it so it doesn't do that. I have thought of a couple of tricks to make that happen but this seems to be a problem that is better understood then quickly patched.

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

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

发布评论

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

评论(5

北渚 2024-12-10 13:53:15

您看到的是 OnKey 被触发两次,第一次是按下按键,第二次是按下按键,因此您必须使用以下命令对其进行过滤

    if (event.getAction()!=KeyEvent.ACTION_DOWN) {
        return true;
    }

    switch (keyCode) {
       case KeyEvent.KEYCODE_1 : 
            //do something
            break;
       case KeyEvent.KEYCODE_2 : 
            //do something
            break;
       case KeyEvent.KEYCODE_3 : 
            //do something
            break;
    }

    return true;

What you are seeing is the OnKey is fired twice, the first time for key down, and the second time for key up, so you have to filter it with

    if (event.getAction()!=KeyEvent.ACTION_DOWN) {
        return true;
    }

    switch (keyCode) {
       case KeyEvent.KEYCODE_1 : 
            //do something
            break;
       case KeyEvent.KEYCODE_2 : 
            //do something
            break;
       case KeyEvent.KEYCODE_3 : 
            //do something
            break;
    }

    return true;
镜花水月 2024-12-10 13:53:15

当条件结果仅导致返回语句时,调试器可能会产生误导。放入一个无用的“int x 变量”,并让它在返回 true 之前指定 x = 2(例如),在返回 false 之前指定 x = 3(例如)。在调试器中再次单步执行,我敢打赌您会看到它只进入其中一个块

The debugger can be misleading when a conditional outcome just leads to a return statement. Put in a useless 'int x variable' and have it assign x = 2 (say) before the return true and x = 3 (say) before the return false. Step through again in the debugger, I'll bet you see it entering only one of the blocks

慢慢从新开始 2024-12-10 13:53:15

尝试使用此代码...

passwordEditText.setOnKeyListener(new OnKeyListener()
{

    public boolean onKey(View v, int keyCode, KeyEvent event) 
    {
        if (keyCode == KeyEvent.KEYCODE_ENTER)
        {
            launch.performClick();
            return true;

        }
        return false;

    }
});

try with this code...

passwordEditText.setOnKeyListener(new OnKeyListener()
{

    public boolean onKey(View v, int keyCode, KeyEvent event) 
    {
        if (keyCode == KeyEvent.KEYCODE_ENTER)
        {
            launch.performClick();
            return true;

        }
        return false;

    }
});
笨笨の傻瓜 2024-12-10 13:53:15

你所描述的情况是不可能的。您发布的代码看起来是正确的,所以我想知道您发布的代码片段中是否存在错误。

也就是说,一些编码错误可能会导致程序员相信“if”和“else”块都正在执行。例如

if(condition) {
  // do something
}
else; // note the semicolon here
{
  // do something else
  // this gets executed regardless of whether the condition is true!
}

,但即使如此,如果你的“if”块中有一个“return”,你也无法到达第二个块:)

What you describe is not possible. The code you posted looks correct, so I wonder if there's an error that's not in the snippet you posted.

That said, some coding mistakes could lead a programmer to believe that both "if" and "else" blocks are being executed. E.g.

if(condition) {
  // do something
}
else; // note the semicolon here
{
  // do something else
  // this gets executed regardless of whether the condition is true!
}

But even then, if you have a "return" in your "if" block, there's no way you could get to the 2nd block :)

一张白纸 2024-12-10 13:53:15

当按下(或按住或释放)某个键时会触发多个事件。

特别是对于按下和释放,会触发以下操作:

ACTION_DOWN

ACTION_DOWN (如果保持,重复计数非零,事件可能重复多次)

ACTION_UP (如果事件被取消,则可能设置 FLAG_CANCELED)

您的代码不会检查操作属性因此每次发送涉及回车键的事件时都会运行。

请替换

if (keyCode == KeyEvent.KEYCODE_ENTER)

if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP)

如果您只想在释放按键时触发一次, 。检查 ACTION_DOWN 需要额外的过滤,以避免由于按键重复而导致多次触发。当 ACTION_UP 发生时,您可能还想检查 FLAG_CANCELED 的状态。

Multiple events are fired when a key is pressed (or held, or released).

Specifically for a press and release the following are fired:

ACTION_DOWN

ACTION_DOWN (if held, with non-zero repeatCount, event possibly repeated multiple times)

ACTION_UP (possibly with the FLAG_CANCELED set if the event was canceled)

Your code does not check the action property and thus will be run every time an event is sent that involves the enter key.

Replace

if (keyCode == KeyEvent.KEYCODE_ENTER)

with

if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP)

if you only want this to fire once, when the key is released. Checking for the ACTION_DOWN requires additional filtering to avoid multiple fires due to key repeating. You probably also want to check the status of the FLAG_CANCELED when the ACTION_UP occurs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文