android: Softkeyboard perform action when Done key is pressed

后端 未结 4 1659
予麋鹿
予麋鹿 2020-12-12 20:05

I have an EditText. I want that after typing some text, when user presses the Done key of the softkeyboard, it should directly perform some search operation whi

相关标签:
4条回答
  • 2020-12-12 20:44

    You catch the KeyEvent and then check its keycode. FLAG_EDITOR_ACTION is used to identify enter keys that are coming from an IME whose enter key has been auto-labelled "next" or "done"

    if (event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION)
        //your code here
    

    Find the docs here.

    Second Method

    myEditText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
        int result = actionId & EditorInfo.IME_MASK_ACTION;
        switch(result) {
        case EditorInfo.IME_ACTION_DONE:
            // done stuff
            break;
        case EditorInfo.IME_ACTION_NEXT:
            // next stuff
            break;
        }
     }
    });
    
    0 讨论(0)
  • 2020-12-12 20:47

    Try this

    editText.setOnEditorActionListener(new OnEditorActionListener() {        
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if(actionId==EditorInfo.IME_ACTION_DONE){
                //do something
            }
        return false;
        }
    });
    
    0 讨论(0)
  • 2020-12-12 20:54

    Try this

    It works both for DONE and RETURN.

    EditText editText= (EditText) findViewById(R.id.editText);
    editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
    
                    @Override
                    public boolean onEditorAction(TextView v, int actionId,
                            KeyEvent event) {
                        if (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER
                                || actionId == EditorInfo.IME_ACTION_DONE) {
                            // Do your action
                            return true;
                        }
                        return false;
                    }
                });
    
    0 讨论(0)
  • 2020-12-12 21:05

    Try this

    This will work in both condition whether your keyboard is showing enter sign or next arrow sign

    YourEdittextName.setOnEditorActionListener(new TextView.OnEditorActionListener()
        {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
            {
                if(actionId== EditorInfo.IME_ACTION_DONE||actionId==EditorInfo.IME_ACTION_NEXT)
                {
                    //Perform Action here 
                }
                return false;
            }
        });
    

    if u r facing red line then do this... import Keyevent and EditorInfo by pressing alt+enter then all the errors remove it will properly.......

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