Android - 键盘未出现在浮动窗口中

2022-01-14 00:00:00 keyboard android java android-edittext

我正在编写一个应用程序,它使用以下代码在正在运行的应用程序的屏幕上绘制编辑文本:

I'm writing an application that uses the following code to draw an edittext on the screen over running applications:

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
                PixelFormat.TRANSLUCENT);

windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

windowManager.addView(mEditText, params);

edittext的xml是:

The xml for the edittext is:

<EditText
            android:id="@+id/mEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="3"
            android:inputType="textAutoComplete|text"
            android:focusable="true"
            android:focusableInTouchMode="true" />

但是,专注于此并不会调出键盘.我还尝试通过 onFocusListener 以编程方式提出它:

However focusing on this does not bring up the keyboard. I've also tried programmatically bringing it up with an onFocusListener:

mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus) {
                    Log.d("", "Has focus");
                    ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
                } else {
                    Log.d("", "Lost focus");
                }
            }
        });

但是,从 logcat 中可以看出,尽管调用了它,但什么也没发生.到目前为止,我发现显示键盘的唯一方法是使用:

But although that is called, as seen from the logcat, nothing happens. The only method I've found so far to display the keyboard is using:

getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(0, 0);

但这似乎是在屏幕上输入而不是在编辑文本中.当显示编辑文本时,我也尝试过清晰可聚焦,但无济于事.

But that seems to type onto the screen and not into the edittext. I've also tried clear focusable when the edittext is displayed but to no avail.

我猜这个问题是因为我使用的是浮动窗口",但必须有一种方法可以使这项工作成为可能,因为 Playstore 中存在浮动计算器等应用程序,它们需要输入.有人有什么想法吗?我被难住了:(

I'm guessing the issue is because I'm using a "floating window" but there must be a way to make this work as apps such as floating calculators exist on the playstore which take input.. Anyone have any ideas? I'm stumped :(

推荐答案

我的错.. 我意识到如果我删除 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE 它可以正常工作.. 愚蠢的错误

My bad.. I realized if I remove the WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE it works fine.. silly mistake

相关文章