Example usage for android.text Editable getSpanStart

List of usage examples for android.text Editable getSpanStart

Introduction

In this page you can find the example usage for android.text Editable getSpanStart.

Prototype

public int getSpanStart(Object tag);

Source Link

Document

Return the beginning of the range of text to which the specified markup object is attached, or -1 if the object is not attached.

Usage

From source file:Main.java

/**
 * Modified from {@link android.text.Html}
 *//*from ww  w. j a  v  a2s  .  co m*/
private static void end(Editable text, Class<?> kind, Object... replaces) {
    int len = text.length();
    Object obj = getLast(text, kind);
    int where = text.getSpanStart(obj);
    text.removeSpan(obj);
    if (where != len) {
        for (Object replace : replaces) {
            text.setSpan(replace, where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}

From source file:org.eyeseetea.malariacare.network.CustomParser.java

/** Modified from {@link android.text.Html} */
private static void end(Editable text, Class<?> kind, Object... replaces) {
    int len = text.length();
    Object obj = getLast(text, kind);
    int where = text.getSpanStart(obj);
    text.removeSpan(obj);/*  w  ww  .jav a 2  s . c o  m*/
    if (where != len) {
        for (Object replace : replaces) {
            text.setSpan(replace, where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
    return;
}

From source file:org.mozilla.focus.widget.InlineAutocompleteEditText.java

/**
 * Get the portion of text that is not marked as autocomplete text.
 *
 * @param text Current text content that may include autocomplete text
 *//*from   w ww .  j  a v a2  s. c  o m*/
private static String getNonAutocompleteText(final Editable text) {
    final int start = text.getSpanStart(AUTOCOMPLETE_SPAN);
    if (start < 0) {
        // No autocomplete text; return the whole string.
        return text.toString();
    }

    // Only return the portion that's not autocomplete text
    return TextUtils.substring(text, 0, start);
}

From source file:org.miaowo.miaowo.util.Html.java

private static void endFont(Editable text) {
    int len = text.length();
    Font f = getLast(text, Font.class);
    int where = text.getSpanStart(f);
    text.removeSpan(f);/*from  w w  w.  ja va 2 s  .co  m*/

    if (where != len) {
        if (!TextUtils.isEmpty(f.mColor)) {
            if (f.mColor.startsWith("@")) {
                Resources res = Resources.getSystem();
                String name = f.mColor.substring(1);
                int colorRes = res.getIdentifier(name, "color", "android");
                if (colorRes != 0) {
                    ColorStateList colors = ResourcesCompat.getColorStateList(res, colorRes, null);
                    text.setSpan(new TextAppearanceSpan(null, 0, 0, colors, null), where, len,
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            } else {
                int c = Color.parseColor(f.mColor);
                if (c != -1) {
                    text.setSpan(new ForegroundColorSpan(c | 0xFF000000), where, len,
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }

        if (f.mFace != null) {
            text.setSpan(new TypefaceSpan(f.mFace), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}

From source file:android.support.text.emoji.EmojiProcessor.java

private static boolean delete(final Editable content, final KeyEvent event, final boolean forwardDelete) {
    if (hasModifiers(event)) {
        return false;
    }/*from   www. ja  va  2  s.com*/

    final int start = Selection.getSelectionStart(content);
    final int end = Selection.getSelectionEnd(content);
    if (hasInvalidSelection(start, end)) {
        return false;
    }

    final EmojiSpan[] spans = content.getSpans(start, end, EmojiSpan.class);
    if (spans != null && spans.length > 0) {
        final int length = spans.length;
        for (int index = 0; index < length; index++) {
            final EmojiSpan span = spans[index];
            final int spanStart = content.getSpanStart(span);
            final int spanEnd = content.getSpanEnd(span);
            if ((forwardDelete && spanStart == start) || (!forwardDelete && spanEnd == start)
                    || (start > spanStart && start < spanEnd)) {
                content.delete(spanStart, spanEnd);
                return true;
            }
        }
    }

    return false;
}

From source file:org.mozilla.focus.widget.InlineAutocompleteEditText.java

/**
 * Remove any autocomplete text//  w ww.j  av  a 2  s.co  m
 *
 * @param text Current text content that may include autocomplete text
 */
private boolean removeAutocomplete(final Editable text) {
    final int start = text.getSpanStart(AUTOCOMPLETE_SPAN);
    if (start < 0) {
        // No autocomplete text
        return false;
    }

    beginSettingAutocomplete();

    // When we call delete() here, the autocomplete spans we set are removed as well.
    text.delete(start, text.length());

    // Keep mAutoCompletePrefixLength the same because the prefix has not changed.
    // Clear mAutoCompleteResult to make sure we get fresh autocomplete text next time.
    mAutoCompleteResult = "";

    // Reshow the cursor.
    setCursorVisible(true);

    endSettingAutocomplete();
    return true;
}

From source file:org.mozilla.focus.widget.InlineAutocompleteEditText.java

/**
 * Convert any autocomplete text to regular text
 *
 * @param text Current text content that may include autocomplete text
 */// w w w . j  a  va  2  s .  c  om
private boolean commitAutocomplete(final Editable text) {
    final int start = text.getSpanStart(AUTOCOMPLETE_SPAN);
    if (start < 0) {
        // No autocomplete text
        return false;
    }

    beginSettingAutocomplete();

    // Remove all spans here to convert from autocomplete text to regular text
    for (final Object span : mAutoCompleteSpans) {
        text.removeSpan(span);
    }

    // Keep mAutoCompleteResult the same because the result has not changed.
    // Reset mAutoCompletePrefixLength because the prefix now includes the autocomplete text.
    mAutoCompletePrefixLength = text.length();

    // Reshow the cursor.
    setCursorVisible(true);

    endSettingAutocomplete();

    // Filter on the new text
    if (mFilterListener != null) {
        mFilterListener.onFilter(text.toString(), null);
    }
    return true;
}

From source file:tv.acfun.a63.CommentsActivity.java

int getQuoteSpanLength(Editable text) {
    Quote quote = TextViewUtils.getLast(text, Quote.class);
    int start = text.getSpanStart(quote);
    int end = text.getSpanEnd(quote);
    if (start >= 0) {
        return end - start;
    }//from   w  ww  . j  a  v a2 s  .  co  m
    return 0;

}

From source file:tv.acfun.a63.CommentsActivity.java

void removeQuote(Editable text) {
    Quote quote = TextViewUtils.getLast(text, Quote.class);
    int start = text.getSpanStart(quote);
    int end = text.getSpanEnd(quote);
    // Log.d(TAG, String.format("start=%d, end=%d", start, end));
    if (start >= 0) {
        // Log.d(TAG, text.subSequence(start, end).toString());
        text.delete(start, end);/*  w  ww . j a va2s .  c  om*/
    }
}

From source file:tv.acfun.a63.CommentsActivity.java

/**
 * call before {@code removeQuote()}/*from ww  w  . j  ava 2  s. c  o m*/
 * 
 * @return -1,if not found
 */
int getQuoteCount() {
    Editable text = mCommentText.getText();
    Quote quote = TextViewUtils.getLast(text, Quote.class);
    int start = text.getSpanStart(quote);
    if (start >= 0) {
        return quote.floosCount;
    }
    return -1;

}