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

前端 未结 5 1257
醉酒成梦
醉酒成梦 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:39

    I am create a custom component who extends AutoCompleteTextView, like in the example below:

    public class PortugueseCompleteTextView extends AutoCompleteTextView {
    ...
    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        if (event != null &&  (event.getKeyCode() == KeyEvent.KEYCODE_BACK)) {
            InputMethodManager inputManager =
                    (InputMethodManager) getContext().
                            getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(
                    this.getWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
        return super.onKeyPreIme(keyCode, event);
    }
    

    I am using this code in the AlertDialog.Builder, but is possible to be using to Activity.

提交回复
热议问题