getExtractedText on inactive InputConnection 警告 android 上的

2022-08-31 07:49:58

我在日志中收到以下警告。

getExtractedText on inactive InputConnection

我无法找到它背后的原因。请帮忙


答案 1

我遇到了类似的问题。我的日志:

W/IInputConnectionWrapper(21214): getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper(21214): getSelectedText on inactive InputConnection
W/IInputConnectionWrapper(21214): getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper(21214): getTextAfterCursor on inactive InputConnection
...
I/Choreographer(20010): Skipped 30 frames!  The application may be doing too much work on its main thread.

我的情况:我有一个用户键入的EditText视图。当用户按下按钮时,EditText 将被清除。当我快速按下按钮时,许多不活动的输入连接条目会流出。

前任:

editText.setText(null);

上面我的logcat中的最后一行很好地表明了正在发生的事情。果然,InputConnection被清除文本的请求所淹没。在尝试清除之前,我尝试修改代码以检查文本长度:

if (editText.length() > 0) {
    editText.setText(null);
}

这有助于缓解问题,因为快速按下按钮不再导致 IInputConnectionWrapper 警告流。但是,当用户在键入内容和按下按钮之间快速交替或在应用程序处于足够负载下时按下按钮等时,这仍然容易出现问题。

幸运的是,我找到了另一种清除文本的方法:Editable.clear()。有了这个,我根本没有收到警告:

if (editText.length() > 0) {
    editText.getText().clear();
}

请注意,如果您希望清除所有输入状态而不仅仅是文本(自动图文集,自动大写,多重点击,撤消),则可以使用TextKeyListener.clear(Editable e)

if (editText.length() > 0) {
    TextKeyListener.clear(editText.getText());
}

答案 2

更新:

我收到 InputConnection 警告的原因不是因为我设置文本的位置(即,在回调中,或)中 - 而是因为我使用的是 .onTextChangedafterTextChangedsetText

我通过致电解决了这个问题:

hiddenKeyboardText.getText().clear();
hiddenKeyboardText.append("some string");

注意:我仍然在回调中进行调用,尽管它也可以在没有警告的情况下工作。afterTextChangedontextChanged

上一个答案:

我在logcat中也收到了相同的消息,尽管我的场景略有不同。我想读取进入EditText的每个字符(或组合字符/粘贴的文本),然后将有问题的EditText重置为默认的初始化字符串。

明文部分按照约翰逊上面的解决方案工作。但是,重置文本有问题,我会收到输入连接警告。

最初,我的定义如下:onTextChanged(CharSequence s, ...)

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    if (isResettingKeyboard)
        return;

    // ... do what needs to be done

    resetKeyboardString();

}

public void resetKeyboardString()
{
    isResettingKeyboard = true;

    hiddenKeyboardText.getText().clear();
    hiddenKeyboardText.setText(keyboardInitString);
    hiddenKeyboardText.setSelection(defaultKeyboardCursorLocation);

    isResettingKeyboard = false;
}

调用 时,EditText 处于只读模式。我不确定这是否意味着我们只能调用它(调用也会产生输入连接警告)。onTextChanged(...)getText.clear()setText(...)

但是,回调是设置文本的正确位置。afterTextChanged(Editable s)

@Override
public void afterTextChanged(Editable s) {

    if (isResettingKeyboard)
        return;

    resetKeyboardString();

    // ... 
}

到目前为止,这是在没有任何警告的情况下工作的。


推荐