Android InputConnectionWrapper 警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12318521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:24:31  来源:igfitidea点击:

InputConnectionWrapper warning

androidwarnings

提问by Leandros

I get an InputConnectionWrapper warning everytime I turn off the screen when my app is visible. I don't know why, because I don't use InputConnection.

每次当我的应用程序可见时关闭屏幕,我都会收到 InputConnectionWrapper 警告。我不知道为什么,因为我不使用InputConnection.

Here is the LogCat Output.

这是 LogCat 输出。

09-07 14:21:31.716: W/IInputConnectionWrapper(24197): getExtractedText on inactive InputConnection
09-07 14:21:31.724: W/IInputConnectionWrapper(24197): getExtractedText on inactive InputConnection
09-07 14:21:31.724: W/IInputConnectionWrapper(24197): getTextBeforeCursor on inactive InputConnection
09-07 14:21:31.724: W/IInputConnectionWrapper(24197): getTextAfterCursor on inactive InputConnection
09-07 14:21:31.724: W/IInputConnectionWrapper(24197): getExtractedText on inactive InputConnection
09-07 14:21:31.724: W/IInputConnectionWrapper(24197): getTextBeforeCursor on inactive InputConnection
09-07 14:21:31.724: W/IInputConnectionWrapper(24197): getTextAfterCursor on inactive InputConnection
09-07 14:21:31.732: W/IInputConnectionWrapper(24197): beginBatchEdit on inactive InputConnection
09-07 14:21:31.732: W/IInputConnectionWrapper(24197): endBatchEdit on inactive InputConnection
09-07 14:21:31.732: W/IInputConnectionWrapper(24197): getExtractedText on inactive InputConnection
09-07 14:21:31.732: W/IInputConnectionWrapper(24197): getTextBeforeCursor on inactive InputConnection
09-07 14:21:31.732: W/IInputConnectionWrapper(24197): getTextAfterCursor on inactive InputConnection
09-07 14:21:31.732: W/IInputConnectionWrapper(24197): beginBatchEdit on inactive InputConnection
09-07 14:21:31.732: W/IInputConnectionWrapper(24197): endBatchEdit on inactive InputConnection
09-07 14:21:31.732: W/IInputConnectionWrapper(24197): getExtractedText on inactive InputConnection
09-07 14:21:32.013: W/IInputConnectionWrapper(24197): showStatusIcon on inactive InputConnection
09-07 14:21:32.013: W/IInputConnectionWrapper(24197): getExtractedText on inactive InputConnection
09-07 14:21:32.021: W/IInputConnectionWrapper(24197): getExtractedText on inactive InputConnection
09-07 14:21:32.021: W/IInputConnectionWrapper(24197): getTextBeforeCursor on inactive InputConnection
09-07 14:21:32.021: W/IInputConnectionWrapper(24197): getTextAfterCursor on inactive InputConnection
09-07 14:21:32.021: W/IInputConnectionWrapper(24197): getExtractedText on inactive InputConnection
09-07 14:21:32.021: W/IInputConnectionWrapper(24197): getTextBeforeCursor on inactive InputConnection
09-07 14:21:32.021: W/IInputConnectionWrapper(24197): getTextAfterCursor on inactive InputConnection
09-07 14:21:32.021: W/IInputConnectionWrapper(24197): beginBatchEdit on inactive InputConnection
09-07 14:21:32.021: W/IInputConnectionWrapper(24197): endBatchEdit on inactive InputConnection
09-07 14:21:32.021: W/IInputConnectionWrapper(24197): getExtractedText on inactive InputConnection
09-07 14:21:32.021: W/IInputConnectionWrapper(24197): getTextBeforeCursor on inactive InputConnection
09-07 14:21:32.021: W/IInputConnectionWrapper(24197): getTextAfterCursor on inactive InputConnection
09-07 14:21:32.028: W/IInputConnectionWrapper(24197): beginBatchEdit on inactive InputConnection
09-07 14:21:32.028: W/IInputConnectionWrapper(24197): endBatchEdit on inactive InputConnection
09-07 14:21:32.028: W/IInputConnectionWrapper(24197): getExtractedText on inactive InputConnection

回答by Paul

guess already too late to help but it is in my case the problem that I have a "setOnEditorActionListener" on an text edit -- if I remove this listener the warning is gone.

猜测已经太晚了,但在我的情况下,问题是我在文本编辑时有一个“ setOnEditorActionListener”——如果我删除这个监听器,警告就会消失。

回答by KronuZ

In my case in the button layout I had this: android:textIsSelectable="true", just removing it and problem solved...

在我的按钮布局中,我有这个:android:textIsSelectable="true",只需将其删除即可解决问题......

回答by lang chen

TextWatcher textWatcherContent = new TextWatcher() {

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        String content = mTxtContent.getText().toString();
        String contact = mTxtContact.getText().toString();
        if (null != mTxtContent && null != mTxtLength) {
            int length = mTxtContent.getText().toString().length();

            if (length > 200) {

                mTxtContent.removeTextChangedListener(textWatcherContent);
                SpannableString spannableString = new SpannableString(content);

                spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#FF3838")), 200
                            , mTxtContent.length() , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                mTxtContent.getText().clear();
                mTxtContent.append(spannableString);
                mTxtContent.setSelection(content.length());

                mTxtContent.addTextChangedListener(textWatcherContent);
            }          
        }
    }
};

回答by Sethu

It turned out that the above usage of the InputConnectionWrapperwas totally correct. However, commitText()gets never called (except for special cases), as there are other methods, which are used during typing. These are mainly setComposingText()and sendKeyEvent().

事实证明,上面的用法InputConnectionWrapper是完全正确的。但是,commitText()永远不会调用(特殊情况除外),因为还有其他方法,它们在键入过程中使用。这些主要是setComposingText()sendKeyEvent()

However, it is also important to override seldom used methods like deleteSurroundingText()or commitText()to make sure to catch every user input, because I ran into a similar issue.

但是,覆盖很少使用的方法(例如deleteSurroundingText()commitText()确保捕获每个用户输入)也很重要,因为我遇到了类似的问题。

My situation: I have an EditTextview the user types into. The EditTextgets cleared when the user presses a button. Lots of inactive InputConnectionentries stream out when I rapidly press the button.

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

E.g. editText.setText(null);

例如 editText.setText(null);

The last line in my logcat above provides a great indication of what is happening. Sure enough, the InputConnectionis overwhelmed with requests to clear the text. I tried modifying the code to check for text length before trying to clear it:

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

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

This helps mitigate the problem in that pressing the button rapidly no longer causes the stream of IInputConnectionWrapperwarnings. However this is still prone to problems when the user rapidly alternates between typing something and pressing the button or presses the button when the app is under sufficient load, etc.

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

Fortunately, I found another way to clear text: Editable.clear(). With this I don't get warnings at all:

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

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

Note that should you wish to clear all input state and not just the text (autotext, autocap, multitap, undo), you can use TextKeyListener.clear(Editable e).

请注意,如果您希望清除所有输入状态而不仅仅是文本(autotext、autocap、multitap、undo),您可以使用TextKeyListener.clear(Editable e).

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

If you want to know more about input connection wrapper please visit the link below.

如果您想了解有关输入连接包装器的更多信息,请访问以下链接。

http://developer.android.com/reference/android/view/inputmethod/InputConnection.html

http://developer.android.com/reference/android/view/inputmethod/InputConnection.html