简体   繁体   English

谷歌键盘搞砸了我的自定义范围

[英]Google keyboard mess up my custom span

Well, I try to build a rich text editor. 好吧,我尝试构建一个富文本编辑器。 I have some buttons to format my editable text (bold, italic, URL, etc.). 我有一些按钮来格式化我的可编辑文本(粗体,斜体,URL等)。
I use Google keyboard with all text correction options enabled ( Settings > Language & input > Google keyboard > Text correction ). 我使用谷歌键盘启用了所有文本更正选项( 设置 > 语言和输入 > 谷歌键盘 > 文本更正 )。

I do the following: 我做以下事情:

In my EditText , I write some text. 在我的EditText ,我写了一些文字。
文本

I select it and apply a bold span with SPAN_EXCLUSIVE_EXCLUSIVE (33) as flags. 我选择它并使用SPAN_EXCLUSIVE_EXCLUSIVE (33)作为标志应用粗体。
选定的文字
在此输入图像描述

Then, I move my cursor to the end. 然后,我将光标移动到最后。
光标在最后

Finally, I add text to the end of the text. 最后,我在文本的末尾添加了文本。 Added text should be without bold. 添加的文字应该没有粗体。
粗体文字

Okay, here is the problem. 好的,这是问题所在。 My bold span flags has changed... Why!? 我的大胆旗帜已经改变......为什么!?

Here is some logs: 这是一些日志:

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

When I use another keyboard, all went fine. 当我使用另一个键盘时,一切都很好。
When I disabled Text correction settings, all went fine. 当我禁用文本修正设置时,一切都很顺利。 All of my span are custom span and subclass an existing Android span. 我的所有范围都是自定义范围,并且是现有Android范围的子类。

It seems Google Keyboard modify my spans by its own (probably because of Show suggestions settings). 谷歌键盘似乎自己修改我的跨度(可能是因为Show suggestions设置)。
How can I avoid this? 我怎么能避免这个?
Maybe I am missing something about span flags? 也许我错过了关于跨度旗帜的事情?

Ok, after some research, it seems the keyboard apply some spans around a word while typing in order to manage suggestions. 好吧,经过一些研究,似乎键盘在输入时会在一个单词周围应用一些跨度以便管理建议。

Problem is for each typed letter, the word is removed and added back with the added letter. 问题出在每个打字的字母上,这个单词被删除并添加回添加的字母。 At this point I loose some custom spans, like those in the middle of the word. 在这一点上,我松开了一些自定义跨度,就像在单词中间那些。

If you add a TextWatcher to your EditText , it will be called 2 times: first with the added letter, second after removed and added back the whole word. 如果将TextWatcher添加到EditText ,它将被调用2次:首先添加字母,第二次删除后添加回整个单词。 Not convenient at all. 根本不方便。

So, an ugly solution is to copy all spans during beforeTextChanged() and apply back during the second afterTextChanged() . 因此,一个丑陋的解决方案是在beforeTextChanged()期间复制所有跨度,并在第二个afterTextChanged()期间应用。 But it is complicated to implement. 但实施起来很复杂。

Anyway, other apps do not do better: GMail and Evernote have same kind of issues. 无论如何,其他应用程序没有做得更好:GMail和Evernote有同样的问题。 I choose to not worry and do not apply the ugly solution. 我选择不担心,不应用丑陋的解决方案。 My rich text editor is usable like this... 我的富文本编辑器可以像这样使用......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM