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

倾然丶 夕夏残阳落幕 提交于 2019-12-01 15:46:00

问题


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 but it seems to have no effect. I also get an 'The method onEditorAction(EditText, int, KeyEvent) from the type new extView.OnEditorActionListener(){} is never used locally' error.

myEditText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
            public boolean onEditorAction(EditText v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_NEXT)
                {

回答1:


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
    }
});



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/4209404/how-do-i-trigger-an-action-when-the-user-has-hit-enter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!