Domanda

I'm trying to solve my problem for 2 days now but without any success.

The problem is: when I set BulletSpan to text and then display it in EditText everything works fine until I start typing in another text. When the text is wrapped at the end of the screen, the indentation works but the cursor is pointing off the actual position and also some characters from previous line are added after the cursor. To better illustrate this problem see the attached image.

Span Error

Also is worth mentioning that this happen only when I type in text, when I'm setting the text in the source and the text is too long to be only on one line the wrapping works fine and no extra characters are added nor the cursor position is wrong.

Also I tried LeadingMarginSpan.Standart and the behaviour was the same.

In code I'm setting the start mark:

private void handleListStart(SpannableStringBuilder text) {
    int len = text.length();
    text.setSpan(new ListItem(), len, len, Spannable.SPAN_MARK_MARK);
}

Then setting the span:

private void handleListEnd(SpannableStringBuilder text) {
    int len = text.length();
    Object obj = getLast(text, ListItem.class);
    int where = text.getSpanStart(obj);

    text.removeSpan(obj);

    if (where != len) {
        text.setSpan(new BulletSpan(listIndent * 15), where, len, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    }
}

I'm getting the data from xml file.

Thanks for any help

EDIT: I forget to add that I have tried this on Android 4.1 and 4.4 and both behaved the same

È stato utile?

Soluzione

This issue happens when your bulletspan-style characters come to a new line.

You can listen when the lines increase, then you can clear the bulletspan and set a new bulletspan again.

The solutiion above works perfectly for me.

Altri suggerimenti

@QuinnChen 's answer worked for me. Let me elaborate it with code for convenience . This issue happens when the text is automatically wrapped to the next line in BulletSpan and LeadingMargin span .

Solution is to remove the previous span and apply the same span again when the line increases .

first set int line_counter = editorEditText.getLineCount(); when you click the button to apply the span

then in the body of textwatcher write this:

if(line_count > editorEditText.getLineCount()){
                        LeadingMarginSpan[] leadingMarginSpans = editorEditText.getText().getSpans(0, editorEditText.getSelectionStart(),
                                LeadingMarginSpan.class);
                        int s , e;
                        for(LeadingMarginSpan ss: leadingMarginSpans){
                            s = editorEditText.getText().getSpanStart(ss);
                            e = editorEditText.getText().getSpanEnd(ss);
                            if(s<=editorEditText.getSelectionStart() && editorEditText.getSelectionStart()<=e){
                                editorEditText.getText().removeSpan(ss);
                                editorEditText.getText().setSpan(new LeadingMarginSpan.Standard(30), s,e,
                                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                                line_count = editorEditText.getLineCount();
                            }

                        }
                    }

This code would execute when the text is automatically wrapped to the next line .

NOTE:

This is the code for LeadingMargin, the solution for bulletSpan goes the same, you just have to change replace LeadingMarginSpan with BulletSpan

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top