好吧,我尝试构建一个富文本编辑器。
我有一些按钮可以设置可编辑文本的格式(粗体,斜体,URL等)。
我在启用所有文本更正选项的情况下使用Google keyboard(设置> 语言和输入> Google键盘> 文本更正)。

我执行以下操作:

在我的EditText中,我写了一些文本。
android - Google键盘弄乱了我的自定义范围-LMLPHP

我选择它并应用带有SPAN_EXCLUSIVE_EXCLUSIVE(33)作为标志的粗体跨度。
android - Google键盘弄乱了我的自定义范围-LMLPHP
android - Google键盘弄乱了我的自定义范围-LMLPHP

然后,我将光标移到末尾。
android - Google键盘弄乱了我的自定义范围-LMLPHP

最后,我在文本末尾添加文本。添加的文字应不使用粗体。
android - Google键盘弄乱了我的自定义范围-LMLPHP

好的,这是问题所在。我的粗体跨度标志已更改...为什么!?

这是一些日志:

D/ContentUtils: beforeTextChanged: start  end  span             flags
D/ContentUtils: beforeTextChanged: 0      7    ChangeWatcher    8388626
D/ContentUtils: beforeTextChanged: 0      7    ChangeWatcher    6553618
D/ContentUtils: beforeTextChanged: 0      7    TextKeyListener  18
D/ContentUtils: beforeTextChanged: 0      7    SpanController   18
D/ContentUtils: beforeTextChanged: 7      7    START            546
D/ContentUtils: beforeTextChanged: 7      7    END              34
D/ContentUtils: beforeTextChanged: 0      7    SpellCheckSpan   33
D/ContentUtils: beforeTextChanged: 0      7    CustomBoldSpan   33

D/ContentUtils: onTextChaghed
D/ContentUtils: onTextChaghed:     0      8    ChangeWatcher    8392722
D/ContentUtils: onTextChaghed:     0      8    ChangeWatcher    6557714
D/ContentUtils: onTextChaghed:     0      8    TextKeyListener  4114
D/ContentUtils: onTextChaghed:     0      8    SpanController   4114
D/ContentUtils: onTextChaghed:     8      8    START            546
D/ContentUtils: onTextChaghed:     8      8    END              34
D/ContentUtils: onTextChaghed:     0      8    CustomBoldSpan   4129
D/ContentUtils: onTextChaghed:     0      8    UnderlineSpan    289
D/ContentUtils: onTextChaghed:     0      8    ComposingText    289

D/ContentUtils: afterTextChanged
D/ContentUtils: afterTextChanged:  0      8    ChangeWatcher    8392722
D/ContentUtils: afterTextChanged:  0      8    ChangeWatcher    6557714
D/ContentUtils: afterTextChanged:  0      8    TextKeyListener  4114
D/ContentUtils: afterTextChanged:  0      8    SpanController   4114
D/ContentUtils: afterTextChanged:  8      8    START            546
D/ContentUtils: afterTextChanged:  8      8    END              34
D/ContentUtils: afterTextChanged:  0      8    CustomBoldSpan   4129
D/ContentUtils: afterTextChanged:  0      8    UnderlineSpan    289
D/ContentUtils: afterTextChanged:  0      8    ComposingText    289
D/ContentUtils: afterTextChanged:  0      8    SpellCheckSpan   33

当我使用另一个键盘时,一切正常。
当我禁用文本更正设置时,一切正常。
我所有的跨度都是自定义跨度,并继承了现有的Android跨度。

看来Google键盘会自行修改我的跨度(可能是因为Show suggestions设置)。
如何避免这种情况?
也许我缺少有关跨度标志的信息?

最佳答案

好的,经过一些研究,看来键盘在输入时在单词周围应用了一些跨度以管理建议。

问题是对于每个键入的字母,单词都会被删除并与添加的字母一起添加回去。此时,我松开了一些自定义范围,例如单词中间的那些。

如果在TextWatcher中添加EditText,它将被调用两次:第一次是添加字母,第二次是删除字母,然后再添加整个单词。根本不方便。

因此,一个丑陋的解决方案是在beforeTextChanged()期间复制所有范围,并在第二个afterTextChanged()期间应用回来。但是实现起来很复杂。

无论如何,其他应用程序并不会做得更好:GMail和Evernote都存在相同的问题。我选择不担心并且不应用丑陋的解决方案。我的RTF编辑器可以这样使用...

关于android - Google键盘弄乱了我的自定义范围,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39493066/

10-11 22:14
查看更多