如何从服务显示软键盘?

发布于 2024-11-28 04:29:18 字数 615 浏览 0 评论 0原文

简短问题:是否可以(以及如何)从服务显示软键盘?

长问题:我编写了一个服务,它创建一个“顶栏”,显示在所有活动的顶部,其中包含一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅沫记忆 2024-12-05 04:29:18

我也遇到过类似的问题。我现在已经解决了。也许为时已晚,但可能对某人有帮助。
可能的错误原因:
在 LayoutParams 期间设置 FLAG_NOT_FOCUSABLE

窗口标志:此窗口永远不会获得按键输入焦点,因此用户
无法向其发送按键或其他按钮事件。那些会转而去
到其后面的任何可聚焦窗口。

只需将 FLAG_NOT_FOCUSABLE 替换为 0

使用以下代码:

InputMethodManager imm = null;

mView.findViewById(R.id.editText).setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    if(imm==null){
                        imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    }

                    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0 );
                }
            }
        });

第二次创建 LayoutParams 时

private WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                    0,
                    PixelFormat.TRANSLUCENT);

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

Window flag: this window won't ever get key input focus, so the user
can not send key or other button events to it. Those will instead go
to whatever focusable window is behind it.

Simply Replace FLAG_NOT_FOCUSABLE with 0

Use this below code:

InputMethodManager imm = null;

mView.findViewById(R.id.editText).setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    if(imm==null){
                        imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    }

                    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0 );
                }
            }
        });

2nd When creating LayoutParams

private WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                    0,
                    PixelFormat.TRANSLUCENT);
浅忆 2024-12-05 04:29:18

我面临类似的问题。但就我而言,问题是由于在为视图创建 WindowManager.LayoutParams 时使用 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE 标志造成的。

以及键盘可见性

InputMethodManager inputManager = (InputMethodManager) context.
                getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputManager != null) {
            inputManager.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT);
        } 

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

InputMethodManager inputManager = (InputMethodManager) context.
                getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputManager != null) {
            inputManager.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT);
        } 
月光色 2024-12-05 04:29:18

如果您想在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文