简体   繁体   中英

Android Spanned object

Suppose I have an EditText:

Editable e = editText.getEditableText(); // length == 2
// ...attach a span  start=0, end=2 to e...

int index = e.nextSpanTransition(0, 2, Object.class);

According to this: http://developer.android.com/reference/android/text/Spanned.html index should be 0, since it says

Return the first offset greater than or equal to start where a markup object of class type begins or ends

But index is 2. Is this a bug or am I missing something?

Or am I even misinterpreting the docs, since it could mean "greater than start where a markup object begins, OR, equal to start where a markup object ends"?

The documentation also says:

or limit if there are no starts or ends greater than or equal to start but less than limit

Where limit is the second parameter (2 in your case). Your span does not satisfy less than limit because it is equal to it. So it returns limit .

Here is the source code that explains it:

/**
 * Return the next offset after <code>start</code> but less than or
 * equal to <code>limit</code> where a span of the specified type
 * begins or ends.
 */
public int nextSpanTransition(int start, int limit, Class kind) {
    int count = mSpanCount;
    Object[] spans = mSpans;
    int[] starts = mSpanStarts;
    int[] ends = mSpanEnds;
    int gapstart = mGapStart;
    int gaplen = mGapLength;

    if (kind == null) {
        kind = Object.class;
    }

    for (int i = 0; i < count; i++) {
        int st = starts[i];
        int en = ends[i];

        if (st > gapstart)
            st -= gaplen;
        if (en > gapstart)
            en -= gaplen;

        if (st > start && st < limit && kind.isInstance(spans[i]))
            limit = st;
        if (en > start && en < limit && kind.isInstance(spans[i]))
            limit = en;
    }

    return limit;
}

Look at the last 2 if -sentences, in your case st=0, start=0, en=2, limit=2. The first if is false, the second if is false too. At the end it returns the unchanged limit parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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