Enter key on EditText hitting onKey twice

前端 未结 7 1871
暖寄归人
暖寄归人 2021-01-07 18:25

I\'ve attached an OnKeyListener to an EditText. I\'ve overrode the onKey handler to capture a KeyEvent.

When a user hits the enter key (either their computer enter

7条回答
  •  一整个雨季
    2021-01-07 19:24

    I had the same issue and the answers above helped me but I am using Xamarin.Android (c#) so it is slightly different syntax.. Here is what worked for me:

    MyStringTextBox.KeyPressed += OnEnterKeyPressed;
    
    protected void OnEnterKeyPressed(object sender, View.KeyEventArgs e)
    {
        if (e.KeyCode == Keycode.Enter && e.Event.Action == KeyEventActions.Up)
        {
            DoSomething(this, EventArgs.Empty);
        }
        else
        {
            e.Handled = false;
        }
    }
    

    This way, the DoSomething() would only be called on hitting Enter Key (Up) only and thus would be fired once. Works and tested on Xamarin.Android

提交回复
热议问题