在android中输入EditText后如何隐藏键盘?
我有一个 EditText
和按钮与父级底部对齐。
当我在其中输入文本并按下按钮保存数据时,虚拟键盘不会消失。
谁能指导我如何隐藏键盘?
I have a EditText
and button aligned to parent's bottom.
When I enter text in it and press the button to save data, the virtual keyboard does not disappear.
Can any one guide me how to hide the keyboard?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
您可能还想在 EditText 中定义 imeOptions。这样,一旦您按下“完成”,键盘就会消失:
You might also want to define the imeOptions within the EditText. This way, the keyboard will go away once you press on Done:
这应该有效。
只需确保 this.getCurrentFocus() 不会返回 null,如果没有焦点,则会返回 null。
This should work.
Just make sure that this.getCurrentFocus() does not return null, which it would if nothing has focus.
并在 XML 中
and in xml
我没有看到有人使用这种方法:
然后只需将焦点请求到 editText:
I did not see anyone using this method:
And then just request focus to the editText:
EditText 操作侦听器中包含的解决方案:
Solution included in the EditText action listenner:
我发现这是因为我的 EditText 在输入时不会自动消失。
这是我原来的代码。
我通过
在用户按回车键时删除该行来解决这个问题。
希望这对某人有帮助。
I found this because my EditText wasn't automatically getting dismissed on enter.
This was my original code.
I solved it by removing the line
after doing stuff when user presses enter.
Hope this helps someone.
只需写下这两行代码即可使用 Enter 选项。
Just write down these two lines of code where enter option will work.
过去几天一直在努力解决这个问题,并找到了一个非常有效的解决方案。当在 EditText 之外的任何地方进行触摸时,软键盘会隐藏。
此处发布的代码:在android中单击时隐藏默认键盘
Been struggling with this for the past days and found a solution that works really well. The soft keyboard is hidden when a touch is done anywhere outside the EditText.
Code posted here: hide default keyboard on click in android
您可以像这样创建一个单例类来轻松调用:
因此,之后可以在活动中调用下一个形式:
you can create a singleton class for call easily like this:
so, after can call in the activity how the next form:
您可以在顶部看到标记的答案。但我使用了 getDialog().getCurrentFocus() 并且运行良好。我发布这个答案是因为我无法在 oncreatedialog 中输入
"this"
。这就是我的答案。如果您尝试了标记答案但不起作用,您可以简单地尝试以下操作:
You can see marked answer on top. But i used
getDialog().getCurrentFocus()
and working well. I post this answer cause i cant type"this"
in my oncreatedialog.So this is my answer. If you tried marked answer and not worked , you can simply try this:
我使用此方法从编辑文本中删除键盘:
并且此方法从活动中删除键盘(在某些情况下不起作用 - 例如,当 edittext 绑定键盘时,失去焦点,它将不起作用。但对于其他情况在某些情况下,它效果很好,并且您不必关心固定键盘的元素)。
I use this method to remove keyboard from edit text:
And this method to remove keyboard from activity (not work in some cases - for example, when edittext, to wich is binded keyboard, lost focus, it won't work. But for other situations, it works great, and you do not have to care about element that holds the keyboard).
editor - 是 EditText 字段
如果您有另一个 EditText 字段,则需要注意焦点变化。
editor - is EditText field
If you have another EditText Field, you need to watch for focus change.
就我而言,为了在按下“发送按钮”时隐藏键盘,我使用了接受的答案,但将上下文更改为 getApplication 并添加了 getWindow()。
In my case, in order to hide the keyboard when pressing the "send button", I used the accepted answer but changed context to getApplication and added getWindow().
隐藏强制完全键盘的最简单的解决方案如下所述
APPROACH 1
JAVA
KOTLIN
APPROACH 2
使用 imeOptions 时,
onEditorAction()
返回false
将在操作完成时隐藏键盘。The simplest solution to hide force fully keyboard is as mentioned below
APPROACH 1
JAVA
KOTLIN
APPROACH 2
While using imeOptions,
onEditorAction()
returningfalse
will hide the keyboard when the action is Done fulfilled.