android set hidden the keyboard on press enter (in a EditText)

前端 未结 5 1156
醉酒成梦
醉酒成梦 2021-01-31 04:20

When my user press Enter on the virtual android \"user validate entry!\" keyboard my keyboard stay visible! (Why?)

Here my Java code...

privat         


        
5条回答
  •  失恋的感觉
    2021-01-31 04:29

    Keep the singleLine="true" and add imeOptions="actionDone" to the EditText. Then in the OnEditorActionListener check if actionId == EditorInfo.IME_ACTION_DONE, like so (but change it to your implementation):

    if (actionId == EditorInfo.IME_ACTION_DONE) {
    
                    if ((username.getText().toString().length() > 0)
                            && (password.getText().toString().length() > 0)) {
                        // Perform action on key press
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(username.getWindowToken(),
                                0);
                        doLogin();
                    }
                }
    

提交回复
热议问题