How to display the Soft Keyboard from a Service?

我只是一个虾纸丫 提交于 2019-12-04 19:07:56

问题


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?


回答1:


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);
        } 



回答2:


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);



回答3:


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.



来源:https://stackoverflow.com/questions/6925267/how-to-display-the-soft-keyboard-from-a-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!