Android 上防止按钮隐藏软键盘
我的布局中有一个 WebView 和一些按钮。我的 WebView 中有一个大标签。该应用程序用于编辑文本文件。这些按钮用于影响我的 WebView 内的文本区域。当用户按下按钮(例如用于移动文本视图的箭头按钮)时,它会关闭键盘。我使用了toggleSoftInput,但这只是切换键盘显示或不显示。我希望按钮在按下按钮时停止隐藏软键盘。我没有发现任何关于我的具体问题的信息。我已经找了几个星期了。有人知道如何阻止我的按钮隐藏 Android 上的软键盘吗?
I have a WebView and some buttons in my layout. There is a large tag in my WebView. This app is used to edit text files. The buttons are used to effect the textarea inside my WebView. When the user presses a button (Such as an Arrow Button to Move the text view) it closes the keyboard. I have used toggleSoftInput, but that just toggles the keyboard to show or not. I want the buttons to stop hiding the soft keyboard when the button is pressed. I have found nothing about my specific problem. I have searched for weeks. Anybody have any idea on how I can stop my Buttons from hiding the Soft Keyboard on Android?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题的解决方案可能是保持键盘始终显示,并让用户在操作完成后将其关闭。
在 Activity 的 onCreate() 方法中尝试此代码。
请注意,如果用户按下键盘上的关闭按钮或后退按钮,则应将其关闭。我想你不应该干涉这种情况。
操作完成后,您可以通过代码关闭键盘。
The solution to your problem might be to keep the keyboard always shown and letting the user to close it when the actions are done.
Try this code in the onCreate() method of your Activity.
Note that if the user presses the close button on the keyboard or the back button it should close it. And I guess you shouldn't interfere with that scenario.
And when the actions are done you can then close the keyboard from code.
我的情况完全一样,所以如果您找到理想的解决方案,我很乐意听到。
编辑:
成功!或者说比以前好多了。我已经删除了旧的解决方案,因为这个更好。
正如 https://stackoverflow.com/a/10536033/513038 中所述,事实证明 loadData() 隐藏了键盘。 但是,我发现在WebView的hideSoftKeyboard()中,它通过imm.isActive(mWebView)检查InputMethodManager以查看webview是否处于活动状态。
因此,如果您在 loadData() 之前将焦点切换到 EditText,并在之后立即切换回 WebView,键盘就会保持不变!它会短暂切换为大写,我认为将焦点返回到网络视图时(实际上,这似乎并不总是发生;这取决于),但它比键盘闪烁来回更不引人注目。
需要发生的事情的要点如下。
扩展 WebView。给它一个 EditText 字段:
在构造函数中,包含以下几行:
然后重写 loadUrl():
基本上,这应该可以使其正常工作。不过,它有点错误,所以这里有一个更完整的类:
I'm in exactly the same boat, so if you ever found an ideal solution, I'd love to hear it.
EDIT:
Success! Or much better than before, anyway. I've deleted my old solution, since this one is better.
As stated in https://stackoverflow.com/a/10536033/513038, it turns out that loadData() hides the keyboard. However, I discovered that in the WebView's hideSoftKeyboard(), it checks the InputMethodManager to see if the webview is active, via imm.isActive(mWebView).
So, if you switch focus to an EditText before loadData(), and switch back to the WebView immediately after, the keyboard sticks around! It briefly switches to upper-case, I think on returning focus to the webview, (actually, this doesn't always seem to happen; it depends) but it's a lot less noticeable than the keyboard flickering away and back.
The gist of what needs to happen is as follows.
Extend WebView. Give it an EditText field:
In the constructor, have the following lines:
Then override loadUrl():
That should get it working, basically. It's a bit buggy, though, so here's a more complete class:
Xamarin Android(替代):
Xamarin Android (Alternative):