Hide Soft keyboard on return key press

前端 未结 9 1200
[愿得一人]
[愿得一人] 2020-12-29 19:00

I\'ve searched half a dozen other answers on SO, but haven\'t found one that works. All I\'m trying to do is dismiss the soft keyboard when the user presses the enter butto

9条回答
  •  隐瞒了意图╮
    2020-12-29 19:43

    Below code works for me.

    IN XML:

        android:imeOptions="actionDone"
        android:inputType="textCapWords"
    

    IN JAVA CLASS:

        edit_text.setOnEditorActionListener(new OnEditorActionListener() {
    
                    @Override
                    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                        // TODO Auto-generated method stub
    
                        if ((actionId==EditorInfo.IME_ACTION_DONE )   )
                         {   
                            //Toast.makeText(getActivity(), "call",45).show();
                           // hide virtual keyboard
                           InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    
                           //or try following:
                           //InputMethodManager imm = (InputMethodManager)getBaseContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                           imm.hideSoftInputFromWindow(auto_data.getWindowToken(), 0);
                           return true;
                        }
                        return false;
    
                    }
                });
    

提交回复
热议问题