How to display the Soft Keyboard from a Service?

后端 未结 3 956
借酒劲吻你
借酒劲吻你 2021-02-14 10:01

Short question: Is it possible (and how) to display the soft-keyboard from a Service?

Long question: I wrote a service which creates a

3条回答
  •  暖寄归人
    2021-02-14 10:23

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

提交回复
热议问题