如何从服务显示软键盘?
简短问题:是否可以(以及如何)从服务显示软键盘?
长问题:我编写了一个服务,它创建一个“顶栏”,显示在所有活动的顶部,其中包含一个 EditText。我想在单击 EditText 时显示软键盘,但这没有发生。
当然,我已经从服务的 onFocusChange() 和 onClick() 中进行了尝试:
InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
我想出的解决方法是通过扩展 Activity 类并添加 AIDL 接口来请求当前活动显示键盘。缺点是每个关键事件都必须发送回服务(通过另一个 AIDL 接口)并手动转换为 Unicode。
此外,如果当前活动包含 EditText,则软键盘仅适用于该活动,并且在选择服务的 EditText 时不再显示。
如果当前活动有 EditText,什么会阻止显示服务的软键盘?会不会是安卓系统的限制?
Short question: Is it possible (and how) to display the soft-keyboard from a Service?
Long question: I wrote a service which creates a "top bar", displayed on top of all activities, containing an EditText. I want to display the soft-keyboard when that EditText is clicked, but this is not happening.
Of course I've tried this from the Service's onFocusChange() and onClick():
InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
The workaround I came up with is to request the current activity to show the keyboard, by extending the Activity class and adding an AIDL interface. The drawback is that each key event has to be sent back to the Service (via another AIDL inteface) and manually converted into Unicode.
Moreover, if the current activity contains an EditText, the soft-keyboard only works for the activity and doesn't show up anymore when the service's EditText is selected.
What prevents the service's soft-keyboard to be displayed if the current activity has an EditText? Could it be an Android limitation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我也遇到过类似的问题。我现在已经解决了。也许为时已晚,但可能对某人有帮助。
可能的错误原因:
在 LayoutParams 期间设置 FLAG_NOT_FOCUSABLE
只需将 FLAG_NOT_FOCUSABLE 替换为 0
使用以下代码:
第二次创建 LayoutParams 时
I have faced a similar issue. I have solved it now. Maybe it's too late but may it helps someone.
Possible Cause of Error:
Setting FLAG_NOT_FOCUSABLE during LayoutParams
Simply Replace FLAG_NOT_FOCUSABLE with 0
Use this below code:
2nd When creating LayoutParams
我面临类似的问题。但就我而言,问题是由于在为视图创建 WindowManager.LayoutParams 时使用 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE 标志造成的。
以及键盘可见性
I am facing similar issue. But in my case, problem is due to use of WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE flag while creating WindowManager.LayoutParams for view.
And for Keyboard visibility
如果您想在 edittext 的触摸上显示软键盘,为什么不考虑将 onTouchListener 与该 edittext 一起使用。我相信 edittext.requestfocus() 也会这样做。如果没有,听众肯定会工作。
此外,对于您提到的那种视图,最好使用片段而不是服务来创建视图。
If you want to show Soft Keyboard on edittext's touch, Why don't you consider using onTouchListener with that edittext. I beleive edittext.requestfocus() will also do this. If not, listener would definately work.
Moreover for the kind of view you mentioning, it would have been best to use fragments instead of service to create view.