closuresbutton上的虚拟键盘
我有一个EditText ,一个button和一个ListView 。 其目的是在EditTextinputsearch屏幕,按下button并使search结果填充该列表。 
这一切都工作完美,但虚拟键盘performance奇怪。
 如果我点击EditText ,我得到虚拟键盘。 如果我点击虚拟键盘上的“完成”button,它将消失。 但是,如果在虚拟键盘上单击“完成”之前单击我的searchbutton,则虚拟键盘将保持不动,而我无法摆脱它。 点击“完成”button不会closures键盘。 它将“完成”button从“完成”更改为箭头,并保持可见。 
谢谢你的帮助
 InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
 我把它放在onClick(View v)事件之后。 
 你需要导入android.view.inputmethod.InputMethodManager ; 
单击button时,键盘将隐藏。
 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) { } } }); 
你应该为你的EditView实现OnEditorActionListener
 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触发隐藏在button中的键盘 
现在单击虚拟键盘上的“完成”和button将执行相同的操作 – 隐藏键盘并执行单击操作。
在button单击事件中添加以下代码:
 InputMethodManager inputManager = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
由于您只有一个编辑文本,因此只需在点击button内为该编辑文本调用操作即可,其余部分由系统处理。 如果你有一个以上的edittext,那么这样做不会那么有效,因为你必须首先得到聚焦的edittext。 但在你的情况下,它会完美的工作
 myedittext.onEditorAction(EditorInfo.IME_ACTION_DONE) 
尝试这个…
- 
显示键盘 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);
这个解决scheme适合我:
 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); } 
您在button点击事件中使用此代码
 // 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); } 
崩溃空点exception修复:我有一种情况,当用户单击button时键盘可能无法打开。 你必须写一个if语句来检查getCurrentFocus()不是null:
  InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); if(getCurrentFocus() != null) { inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
 View view = this.getCurrentFocus(); if (view != null) { InputMethodManager imm =(InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0);enter code here} 
对于活动,
 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); 
 如果你设置android:singleLine="true" ,自动button隐藏键盘