2

I want to implement a effect that control input characters in edittext whether use span.

For example, in EditText can use

getEditableText().setSpan(new StyleSpan(Typeface.NORMAL), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

make selected characters be BOLD.Is There have solutions that when user enable Bold setting, every character input since then will be bold, and once user disable Bold setting, characters input after that will not be bold.

I tried use Spanned.SPAN_EXCLUSIVE_INCLUSIVE as flags in SpannableStringBuilder.setSpan(), but I just can control when span's effect start, and can't control end.

Also tried use TextWatcher to listen every character input, and set Span to every character.

 @Override
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter)
{
    setTextSpan(start, lengthBefore, lengthAfter);
}

private void setTextSpan(int start, int lengthBefore, int lengthAfter)
{
    if (isBold) {
        getEditableText().setSpan(new StyleSpan(Typeface.BOLD), start, start + lengthAfter, Spanned
                .SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}

but when use Html.toHtml(), every character be wrapped by <b></b>.

Like<p dir="ltr"><b>1</b><b>1</b><b>1</b><b>1</b><b>1</b><b>1</b></p>

I read some source code in SpannableStringBuilder.java, but do not find any method to change span's effect scope.

Hope someone can give me some suggestions. Thanks in advance.

  • How about TextWatcher that you used, what is the problem? – Holi Boom May 10 '16 at 03:56
  • @Huo Chhunleng I'm sorry to reply so late.The problem is, when I use TextWatcher and Input some characters, the display is right, but after convert by *Html.toHtml()*, the result isn't what I want.For example, Input:abdce, after convert I'll obtain '

    abcde

    ', but I desired result is '

    abcde

    '
    – nebulae-pan May 10 '16 at 10:36
  • https://akashkubavat.wordpress.com/tag/remove-underline-from-edittext/ – Holi Boom May 11 '16 at 04:47
  • @Huo Chhunleng In this way ,just can set the selected text bold.The effect I want is that when enable the bold,all characters input after are bold,disable bold , input is normal. – nebulae-pan May 11 '16 at 12:16
  • Sorry to interrupt you, have you resolved the problem? Or find other way to achieve? If so, high appreciate for the reply. – Gallery Mar 14 '18 at 08:30

2 Answers2

0

Some useful advices have been found, you can check 'Android EditText: how to apply spans when composing?'.

But, there is also another question:The flag 'SPAN_INCLUSIVE_INCLUSIVE' setting font BOLD works well. But, when I switch to set the font NOT BOLD(just return to normal), the text insertted is still BOLD. I don't know how to resolve this problem.

Gallery
  • 268
  • 4
  • 14
  • Hi,I have already read the answer you mentioned,that solution can handle some problems, but the situation I encountered is more complicated. I finally thought of two solutions.I will describe these in my answer。 – nebulae-pan Mar 16 '18 at 08:04
0

First of all,set TextWatcher for EditText. Then there are two solutions.
1. when text changed, if input character, use removeSpan(), then use setSpan() reset the span's range.
2. Use reflect. SpannableStringBuilder has member call mSpanEnds, this array record position information for all span in the SpannableStringBuilder,use reflect change it's value.
I have a sample for solution 2,this sample may have some bugs,but it works well in apply spans.