Android Text spaces disappear when pressed in the opposite direction and act as a whole

I need to implement wysiwyg EditText, and I ran into a problem right at the beginning. So I added the following code to onTextChanged:

int textLen = textWord.getText().length();
if (s.length() > textWordBefore.length()) {
    if (boldToggle == true) {
        textWord.getText().setSpan(new StyleSpan(android.graphics.Typeface.BOLD), textLen-1, textLen, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}

I converted the stretched text to Html to see how it works. It works great when I add characters. At first it looks like this:

<b>a</b><b>b</b><b>c</b>

Then I press the spacebar and it changes to this:

<b>abc</b>

But when I press back for the first time, only the last character of the word remains bold. And one more time the whole word becomes normal (i.e. Not bold). I searched Google a lot, tried to reapply the range when the backspace was pressed (if the text after the change is shorter). I tried changing the span type to INCLUSIVE etc. I tried DroidWriter and it works the same way as with backspace.

, , .

, . !

+4

Source: https://habr.com/ru/post/1538174/


All Articles