如何以编程方式关闭Android Soft KeyBoard?
我目前正在使用以下代码显示软键盘
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN);
在这里,我没有使用Edittext绑定软键盘,因为我使用了上面的代码。
现在,我想关闭SoftKeyboard,所以我目前正在使用以下代码,但是它不起作用。
imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN);
有人可以建议我使用什么来关闭softKeyboard吗?
基于下面的答案,我想让您明确地说,我没有使用EditText,而是使用了要在其上显示键盘和隐藏键盘的布局。 我想将键盘按键事件发送到我未使用editText的远程区域bcoz。
13个解决方案
94 votes
我已经测试过,并且可以正常工作:
...
//to show soft keyboard
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
//to hide it, call the method again
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
顺便说一句,您的代码的第二个参数不正确,请在这里看看。
answered 2020-02-02T20:24:59Z
40 votes
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(EditTextName.getWindowToken(), 0);
Jana answered 2020-02-02T20:25:15Z
32 votes
使用此工作代码:
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
Reddy Raaz answered 2020-02-02T20:25:34Z
9 votes
如果需要,可以使用整个类,并在任何地方调用KeyboardUtil.hideKeyBoard(context)方法:
public class KeyboardUtil
{
public static void hideKeyboard(Activity activity)
{
try
{
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
catch (Exception e)
{
// Ignore exceptions if any
Log.e("KeyBoardUtil", e.toString(), e);
}
}
}
Reddy Raaz answered 2020-02-02T20:25:54Z
3 votes
关闭/隐藏Android软键盘
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
it's working for me i hope it's work for you..
打开Android软键盘
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
Ashutosh Srivastava answered 2020-02-02T20:26:19Z
2 votes
user942821隐藏它的答案有效:
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
但这对我也可以隐藏它:
imm.toggleSoftInput(0, 0);
您可能还想尝试:
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
在第一个参数中使用'0'时,有时在奇怪的情况下键盘会在错误的位置打开,而我还无法弄清楚如何进行复制。 我仍在测试最后一个示例,但是当我发现更多示例时,它将进行更新。
有关更多信息,请参见toggleSoftInput文档页面。
uowaep answered 2020-02-02T20:26:56Z
2 votes
这很好
InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(getWindow().getAttributes().token, 0);
Daniel Cherubini answered 2020-02-02T20:27:16Z
0 votes
您也可以尝试
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
mseo answered 2020-02-02T20:27:36Z
0 votes
这是s解决方案,它检查键盘是否可见
public static void hideKeyboard(Activity activity) {
if (isKeyboardVisible(activity)) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}
}
public static boolean isKeyboardVisible(Activity activity) {
///This method is based on the one described at http://stackoverflow.com/questions/4745988/how-do-i-detect-if-software-keyboard-is-visible-on-android-device
Rect r = new Rect();
View contentView = activity.findViewById(android.R.id.content);
contentView.getWindowVisibleDisplayFrame(r);
int screenHeight = contentView.getRootView().getHeight();
int keypadHeight = screenHeight - r.bottom;
return
(keypadHeight > screenHeight * 0.15);
}
Boris Treukhov answered 2020-02-02T20:27:56Z
0 votes
InputMethodManager im =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
Priyanka answered 2020-02-02T20:28:11Z
0 votes
为了隐藏键盘,
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mView.getWindowToken(), 0);
在这里,“ mView”可以是屏幕上可见的任何视图
varotariya vajsi answered 2020-02-02T20:28:35Z
0 votes
此代码从AutoCompleteTextView的onItemClick内部隐藏键盘
public void onItemClick(AdapterView> adapterViewIn, View viewIn, int indexSelected, long arg3) {
// whatever your code does
InputMethodManager imm = (InputMethodManager) getSystemService(viewIn.getContext().INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(viewIn.getApplicationWindowToken(), 0);
}
tony gil answered 2020-02-02T20:28:55Z
-2 votes
private void close() {
this.requestHideSelf(0);
}
这个方法很简单
Vincent Williams answered 2020-02-02T20:29:15Z