How do I trigger an action when the user has hit enter?

后端 未结 3 1596
梦毁少年i
梦毁少年i 2021-01-18 05:40

If (in Android) I have an EditText box, how can I trigger an event when the user has finished entering data and hits return/Next?

I have tried using the code below b

相关标签:
3条回答
  • 2021-01-18 06:21

    Add a KeyListener to the textbox that listens key events. Within the key event, listen to the "ENTER" key pressed and perform your action.

    You can find something similar here: Android Development: How To Use onKeyUp? and in the docs for UI-Events.

    A good example is in the linked SO question. Following this, you should get the desired result.

    0 讨论(0)
  • 2021-01-18 06:22

    I played around a bit with various things and found that the code below works:

    myEditText.setOnEditorActionListener(new OnEditorActionListener() {                     
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            // Do some stuff
        }
    });
    
    0 讨论(0)
  • 2021-01-18 06:33

    I believe you want is the setOnEditorActionListener() method, dev guide info here on the method.

    0 讨论(0)
提交回复
热议问题