Android 隐藏软键盘不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2796231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:25:47  来源:igfitidea点击:

Hide Soft Keyboard Not Working

androidhidesoft-keyboard

提问by stormin986

I'm developing on the Droid Incredible (and have tested on a 1.5 AVD Emulator as well), and one of the tabs in my tab widget consists of a listview and a row with an EditText and a Send button (for a chat feature). I am using the following to close the soft keyboard once I click Send, but it's not working. This is identical to code I've found elsewhere that people have upvoted as correct.

我正在 Droid Incredible 上进行开发(并且也在 1.5 AVD Emulator 上进行了测试),并且我的选项卡小部件中的一个选项卡包含一个列表视图和一个带有 EditText 和发送按钮的行(用于聊天功能) . 单击“发送”后,我正在使用以下命令关闭软键盘,但它不起作用。这与我在其他地方发现的人们认为正确的代码相同。

See anything I'm missing?

看到我遗漏的东西了吗?

// in Button's onClick():
EditText chatTextBox = (EditText) findViewById(R.id.chat_entry);
// Handle button click ...
chatTextBox.setText("");

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(chatTextBox.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);

I also tried changing the flag to 0. No luck. Anyone know what's up??

我也尝试将标志更改为 0。不走运。有谁知道怎么回事??

****EDIT**** Just realized I was originally using hideSoftInputFromInputMethod()instead of hideSoftInputFromWindow(). Changing it didn't make it work though...

****编辑**** 刚刚意识到我最初使用的是hideSoftInputFromInputMethod()而不是hideSoftInputFromWindow(). 改变它并没有使它起作用...

回答by stormin986

Changing HIDE_IMPLICIT_ONLY to 0 did it (after I changed tohideSoftInputFromWindow()from of hideSoftInputFromInputMethod()).

将 HIDE_IMPLICIT_ONLY 更改为 0 做到了(在我更改为hideSoftInputFromWindow()from of 之后hideSoftInputFromInputMethod())。

However I'm not sure why HIDE_IMPLICIT_ONLY isn't working since I am not explicitly opening the keyboard with a long press on Menu.

但是我不确定为什么 HIDE_IMPLICIT_ONLY 不起作用,因为我没有通过长按菜单明确打开键盘。

回答by Joseph Selvaraj

Another option to prevent it from activity in AndroidManifest.xml file

阻止它在 AndroidManifest.xml 文件中活动的另一个选项

android:windowSoftInputMode="stateAlwaysHidden" - This method will prevent loading/showing keyboard when the activity is loaded. But when you click the editable component like edittext the keyboard will open. perfect for my requirement.

android:windowSoftInputMode="stateAlwaysHidden" - 此方法将在加载 Activity 时阻止加载/显示键盘。但是当您单击诸如 edittext 之类的可编辑组件时,键盘将打开。非常适合我的要求。

<activity
            android:name=".Name"
            android:label="@string/app_name" 
            android:windowSoftInputMode="stateAlwaysHidden">

回答by Uday Sravan K

1.first bind your edit text token with keyboard and open
i.e inputMethodManager.showSoftInput(_edittext, 0);//here _edittext is instance of view

1.首先用键盘绑定你的编辑文本标记并打开
inputMethodManager.showSoftInput(_edittext, 0);//这里_edittext是视图的实例

2.keyboard will get hidden automatically if the edit text goes hidden from the screen

2.如果编辑文本从屏幕上隐藏,键盘将自动隐藏

3.edit text is still on screen but you want to hide the keyboard then use below code imm.hideSoftInputFromWindow(_edittext.getWindowToken(), 0);//this won't work if edittext is not on screen or not focused.

3.edit text 仍然在屏幕上,但你想隐藏键盘然后使用下面的代码 imm.hideSoftInputFromWindow(_edittext.getWindowToken(), 0);//如果 edittext 不在屏幕上或没有聚焦,这将不起作用。