How to close Android Soft KeyBoard programmaticall

2019-01-10 22:05发布

I am currently showing softkeyboard using the following code

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN);

And Here I d'not bind the softkeyboard with Edittext because of that I had used the above code.

Now I want to close the SoftKeyboard so i am currently using the below code but it is not working.

imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN);

Can Anyone suggest me what to use for closing the softKeyboard ?


Based on Below Answer I want to let you clear that I am not using EditText, I use Layout on which I want to show Keyboard and Hide keyboard. I want to send keyboard key event to remote area bcoz of that I didnot used editText.

13条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-01-10 22:35

This code hides the keyboard from inside onItemClick of an AutoCompleteTextView

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);
}
查看更多
虎瘦雄心在
3楼-- · 2019-01-10 22:36
InputMethodManager im =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
查看更多
做个烂人
4楼-- · 2019-01-10 22:37

You could also try

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
查看更多
We Are One
5楼-- · 2019-01-10 22:37

For hiding keyboard,

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mView.getWindowToken(), 0);

Here, “mView” can be any view which is visible on screen

查看更多
Bombasti
6楼-- · 2019-01-10 22:38

user942821's answer for hiding it works:

imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

But this also works for me to hide it:

imm.toggleSoftInput(0, 0);

You might also want to try:

imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);

When using '0' in the first parameter sometimes the keyboard toggles on in the wrong places under weird circumstances that I haven't been able to figure out how to duplicate yet. I'm still testing this last example, but will update when I find out more.

See toggleSoftInput documentation page for more information.

查看更多
太酷不给撩
7楼-- · 2019-01-10 22:48

Use this working code :

InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
查看更多
登录 后发表回答