按下按钮时关闭虚拟键盘
我有一个带有 EditText
、一个按钮和一个 ListView
的 Activity
。目的是在 EditText
中键入搜索屏幕,按下按钮并使搜索结果填充此列表。
这一切都工作正常,但虚拟键盘的行为很奇怪。
如果我点击EditText
,我就会得到虚拟键盘。如果我单击虚拟键盘上的“完成”按钮,它就会消失。但是,如果我在单击虚拟键盘上的“完成”之前单击搜索按钮,则虚拟键盘将保留并且我无法摆脱它。单击“完成”按钮不会关闭键盘。它将“完成”按钮从“完成”更改为箭头并保持可见。
感谢您的帮助
I have an Activity
with an EditText
, a button and a ListView
. The purpose is to type a search screen in the EditText
, press the button and have the search results populate this list.
This is all working perfectly, but the virtual keyboard is behaving strange.
If I click the EditText
, I get the virtual keyboard. If I click the "Done" button on the virtual keyboard, it goes away. However, if I click my search button before clicking "Done" on the virtual keyboard, the virtual keyboard stays and I can't get rid of it. Clicking the "Done" button does not close the keyboard. It changes the "Done" button from "Done" to an arrow and remains visible.
Thanks for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
我将其放在
onClick(View v)
事件之后。您需要导入
android.view.inputmethod.InputMethodManager
;单击按钮时键盘会隐藏。
I put this right after the
onClick(View v)
event.You need to import
android.view.inputmethod.InputMethodManager
;The keyboard hides when you click the button.
使用下面的代码
Use Below Code
您应该为您的 EditView 实现
OnEditorActionListener
并且通过以下方式隐藏键盘:
您还应该使用
onClickListener
在按钮中触发键盘隐藏现在单击虚拟键盘和按钮上的“完成”即可相同 - 隐藏键盘并执行单击操作。
You should implement
OnEditorActionListener
for your EditViewAnd you hide keyboard by:
You should also fire keyboard hiding in your button using
onClickListener
Now clicking 'Done' on virtual keyboard and button will do the same - hide keyboard and perform click action.
对于 Activity、
Fragment,请使用 getActivity()
getActivity().getSystemService();
getActivity().getCurrentFocus();
For Activity,
For Fragments, use getActivity()
getActivity().getSystemService();
getActivity().getCurrentFocus();
就您而言,由于您只有一个 EditText,因此我相信以下解决方案是最好的解决方案。如果您有多个 EditText 组件,那么您需要关注要关闭的 EditText,或者为每个 EditText 调用以下函数。但对您来说,这非常有效:
基本上,EditText 已经具有一个函数,用于处理使用键盘后要执行的操作。我们只是传递我们想要关闭键盘。
In your case, as you only have one EditText, the bellow solution is the best one, I believe. If you had more than one EditText component, then either you would need to focus on the EditText that you wanted to close, or call the bellow function for every EditText. For you though, this works brilliantly:
Basically, the EditText already has a function that is intended to deal with what to do after the keyboard has been used. We just pass that we want to close the keyboard.
在按钮单击事件中添加以下代码:
Add the following code inside your button click event:
这个解决方案非常适合我:
This solution works perfect for me:
试试这个...
用于显示键盘
用于隐藏键盘
Try this...
For Showing keyboard
For Hide keyboard
Kotlin 示例:
来自 Fragment:
来自 Activity:
Kotlin example:
from Fragment:
from Activity:
您在按钮单击事件中使用此代码
You use this code in your button click event
崩溃空点异常修复:
我遇到过这样的情况:当用户单击按钮时键盘可能无法打开。您必须编写一个 if 语句来检查 getCurrentFocus() 是否不为空:
Crash Null Point Exception Fix:
I had a case where the keyboard might not open when the user clicks the button. You have to write an if statement to check that getCurrentFocus() isn't a null:
如果设置
android:singleLine="true"
,按钮会自动隐藏键盘¡If you set
android:singleLine="true"
, automatically the button hides the keyboard¡