Hide Soft Keyboard on Done Keypress in Android?

[亡魂溺海] 提交于 2019-12-02 22:04:51

Use android:imeOptions="actionDone", like that:

<EditText
    ...
    android:imeOptions="actionDone" />
InputMethodManager inputManager = (InputMethodManager)
context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.toggleSoftInput(0, 0);

with context being your activity.

Changed the if-statement to if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) made it working with the xml-attribute android:inputType="phone".

You should have a look at setOnEditorActionListener() for the EditText:

Set a special listener to be called when an action is performed on the text view. This will be called when the enter key is pressed, or when an action supplied to the IME is selected by the user.

Use below code with android:imeOptions="actionDone" its work for me.

 <EditText
    android:id="@+id/et_switch_name"       
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="Name"
    android:imeOptions="actionDone"       
    android:inputType="textPersonName" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!