关闭按钮按下虚拟键盘

我有一个 Activity和一个 EditText,一个按钮和一个 ListView。目的是在 EditText中键入一个搜索屏幕,按下按钮,然后将搜索结果填充到这个列表中。

这一切都工作得很完美,但虚拟键盘的行为却很奇怪。

如果我单击 EditText,就会得到虚拟键盘。如果我点击虚拟键盘上的“完成”按钮,它就会消失。但是,如果我在虚拟键盘上点击“完成”之前单击搜索按钮,虚拟键盘就会保持不动,我就无法摆脱它。单击“完成”按钮不会关闭键盘。它将“完成”按钮从“完成”更改为箭头并保持可见。

谢谢你的帮助

142526 次浏览

应该为 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触发隐藏在按钮中的键盘

现在,点击虚拟键盘和按钮上的“完成”将执行相同的隐藏键盘和点击动作。

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;
}
});
InputMethodManager inputManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);


inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);

我把这个放在 onClick(View v)事件之后。

你需要进口 android.view.inputmethod.InputMethodManager;

当你点击按钮时,键盘会隐藏起来。

使用以下代码

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) {


}
}
});

在按钮单击事件中添加以下代码:

InputMethodManager inputManager = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

如果设置 android:singleLine="true",按钮会自动隐藏键盘

在按钮单击事件中使用此代码

// 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);
}

试试这个..。

  1. 展示键盘

    editText.requestFocus();
    InputMethodManager imm = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    
  2. For Hide keyboard

    InputMethodManager inputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
    inputManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
    

在您的情况下,由于您只有一个 EditText,我相信下面的解决方案是最好的。如果您有多个 EditText 组件,那么您需要关注您想要关闭的 EditText,或者为每个 EditText 调用下面的函数。不过,对你来说,这个方法非常有效:

editText.onEditorAction(EditorInfo.IME_ACTION_DONE);

基本上,EditText 已经有了一个函数,用于处理使用键盘后的操作。我们只是通过,我们想要关闭键盘。

View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm =(InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);enter code here}

崩溃零点异常修正: 我遇到过这样的情况,当用户单击按钮时,键盘可能无法打开。您必须编写一个 if 语句来检查 getCurrentFocus ()是否为 null:

            InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if(getCurrentFocus() != null) {
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);
}

活动方面:

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);

科特林的例子:

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)