我有一个Activity
带有EditText
一个按钮和一个ListView
。目的是在 中键入搜索屏幕EditText
,按下按钮并让搜索结果填充此列表。
这一切都很好,但虚拟键盘的行为很奇怪。
如果我点击EditText
,我会得到虚拟键盘。如果我单击虚拟键盘上的“完成”按钮,它就会消失。但是,如果我在单击虚拟键盘上的“完成”之前单击搜索按钮,则虚拟键盘会保留下来,我无法摆脱它。单击“完成”按钮不会关闭键盘。它将“完成”按钮从“完成”更改为箭头并保持可见。
谢谢你的帮助
我有一个Activity
带有EditText
一个按钮和一个ListView
。目的是在 中键入搜索屏幕EditText
,按下按钮并让搜索结果填充此列表。
这一切都很好,但虚拟键盘的行为很奇怪。
如果我点击EditText
,我会得到虚拟键盘。如果我单击虚拟键盘上的“完成”按钮,它就会消失。但是,如果我在单击虚拟键盘上的“完成”之前单击搜索按钮,则虚拟键盘会保留下来,我无法摆脱它。单击“完成”按钮不会关闭键盘。它将“完成”按钮从“完成”更改为箭头并保持可见。
谢谢你的帮助
InputMethodManager inputManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
我把这个放在onClick(View v)
事件之后。
您需要导入android.view.inputmethod.InputMethodManager
;
单击按钮时键盘会隐藏。
mMyTextView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
// hide virtual keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(m_txtSearchText.getWindowToken(),
InputMethodManager.RESULT_UNCHANGED_SHOWN);
return true;
}
return false;
}
});
使用下面的代码
your_button_id.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
}
}
});
您应该OnEditorActionListener
为您的 EditView实施
public void performClickOnDone(EditView editView, final View button){
textView.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(EditView v, int actionId, KeyEvent event) {
hideKeyboard();
button.requestFocus();
button.performClick();
return true;
}
});
你通过以下方式隐藏键盘:
public void hideKeybord(View view) {
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),
InputMethodManager.RESULT_UNCHANGED_SHOWN);
}
您还应该使用隐藏在按钮中的键盘onClickListener
现在单击虚拟键盘和按钮上的“完成”将执行相同的操作 - 隐藏键盘并执行单击操作。
在您的情况下,由于您只有一个 EditText,我相信下面的解决方案是最好的解决方案。如果您有多个 EditText 组件,那么您需要专注于要关闭的 EditText,或者为每个 EditText 调用波纹管函数。不过,对你来说,这非常有效:
editText.onEditorAction(EditorInfo.IME_ACTION_DONE);
基本上,EditText 已经有一个函数,用于处理使用键盘后要做什么。我们只是传递了我们想要关闭键盘的信息。
对于活动,
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
对于片段,使用getActivity()
getActivity().getSystemService();
getActivity().getCurrentFocus();
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
在按钮单击事件中添加以下代码:
InputMethodManager inputManager = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
这个解决方案非常适合我:
private void showKeyboard(EditText editText) {
editText.requestFocus();
editText.setFocusableInTouchMode(true);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.RESULT_UNCHANGED_SHOWN);
editText.setSelection(editText.getText().length());
}
private void closeKeyboard() {
InputMethodManager inputManager = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
}
试试这个...
用于显示键盘
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
对于隐藏键盘
InputMethodManager inputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm =(InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);enter code here}
科特林示例:
val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
从片段:
inputMethodManager.hideSoftInputFromWindow(activity?.currentFocus?.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
从活动:
inputMethodManager.hideSoftInputFromWindow(currentFocus?.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
您在按钮单击事件中使用此代码
// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
崩溃空点异常修复:我遇到了当用户单击按钮时键盘可能无法打开的情况。您必须编写一个 if 语句来检查 getCurrentFocus() 是否为空:
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if(getCurrentFocus() != null) {
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
如果设置android:singleLine="true"
,按钮自动隐藏键盘¡