1
简单的问题 - 如何在点击回车键/按钮后关闭虚拟键盘?按Enter键后隐藏键盘
我试过这个Handle “Enter” key on Jelly Bean和How to hide keyboard on enter key,但这些都不适合我。
简单的问题 - 如何在点击回车键/按钮后关闭虚拟键盘?按Enter键后隐藏键盘
我试过这个Handle “Enter” key on Jelly Bean和How to hide keyboard on enter key,但这些都不适合我。
你有两种选择。
使用XML:
<EditText
android:id="@+id/editText1"
android:inputType="text"
android:imeOptions="actionDone"/>
与代码。
edittext.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(edittext.getApplicationWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
}
return false;
}
});
XML代码有效,但Java代码无效。仍然想知道为什么不。 – MatusMak
第二个链接中的代码应该工作,我将提供相同的答案。当你尝试时会发生什么? – Coderji
由于某种原因没有任何结果,输入密钥就像通常那样工作。 – MatusMak