Java Code Examples for android.text.Editable#getSpanStart()

The following examples show how to use android.text.Editable#getSpanStart() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TokenCompleteTextView.java    From TokenAutoComplete with Apache License 2.0 6 votes vote down vote up
/**
 * Set the count span the current number of hidden objects
 */
private void updateCountSpan() {
    //No count span with free form text
    if (!preventFreeFormText) { return; }

    Editable text = getText();

    int visibleCount = getText().getSpans(0, getText().length(), TokenImageSpan.class).length;
    countSpan.setCount(getObjects().size() - visibleCount);

    SpannableStringBuilder spannedCountText = new SpannableStringBuilder(countSpan.getCountText());
    spannedCountText.setSpan(countSpan, 0, spannedCountText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    internalEditInProgress = true;
    int countStart = text.getSpanStart(countSpan);
    if (countStart != -1) {
        //Span is in the text, replace existing text
        //This will also remove the span if the count is 0
        text.replace(countStart, text.getSpanEnd(countSpan), spannedCountText);
    } else {
        text.append(spannedCountText);
    }

    internalEditInProgress = false;
}
 
Example 2
Source File: ConverterHtmlToText.java    From Android-RTEditor with Apache License 2.0 6 votes vote down vote up
/**
 * When we come upon an ignored tag, we mark it with an Annotation object with a specific key
 * and value as above. We don't really need to be checking these values since Html.fromHtml()
 * doesn't use Annotation spans, but we should do it now to be safe in case they do start using
 * it in the future.
 *
 * @param opening If this is an opening tag or not.
 * @param output  Spannable string that we're working with.
 */
private void handleIgnoredTag(boolean opening, Editable output) {
    int len = output.length();
    if (opening) {
        output.setSpan(new Annotation(IGNORED_ANNOTATION_KEY, IGNORED_ANNOTATION_VALUE), len,
                len, Spanned.SPAN_MARK_MARK);
    } else {
        Object start = getOpeningAnnotation(output);
        if (start != null) {
            int where = output.getSpanStart(start);
            // Remove the temporary Annotation span.
            output.removeSpan(start);
            // Delete everything between the start of the Annotation and the end of the string
            // (what we've generated so far).
            output.delete(where, len);
        }
    }
}
 
Example 3
Source File: MentionsEditText.java    From Spyglass with Apache License 2.0 6 votes vote down vote up
/**
 * Temporarily remove MentionSpans that may interfere with composing text. Note that software keyboards are allowed
 * to place arbitrary spans over the text. This was resulting in several bugs in edge cases while handling the
 * MentionSpans while composing text (with different issues for different keyboards). The easiest solution for this
 * is to remove any MentionSpans that could cause issues while the user is changing text.
 *
 * Note: The MentionSpans are added again in {@link #replacePlaceholdersWithCorrespondingMentionSpans(Editable)}
 *
 * @param text the current text before it changes
 */
private void replaceMentionSpansWithPlaceholdersAsNecessary(@NonNull CharSequence text) {
    int index = getSelectionStart();
    int wordStart = findStartOfWord(text, index);
    Editable editable = getText();
    MentionSpan[] mentionSpansInCurrentWord = editable.getSpans(wordStart, index, MentionSpan.class);
    for (MentionSpan span : mentionSpansInCurrentWord) {
        if (span.getDisplayMode() != Mentionable.MentionDisplayMode.NONE) {
            int spanStart = editable.getSpanStart(span);
            int spanEnd = editable.getSpanEnd(span);
            editable.setSpan(new PlaceholderSpan(span, spanStart, spanEnd),
                    spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
            editable.removeSpan(span);
        }
    }
}
 
Example 4
Source File: HtmlTagHandler.java    From v2ex with Apache License 2.0 6 votes vote down vote up
private void end(Editable output, Class kind, Object repl, boolean paragraphStyle) {
    Object obj = getLast(output, kind);
    // start of the tag
    int where = output.getSpanStart(obj);
    // end of the tag
    int len = output.length();

    output.removeSpan(obj);

    if (where != len) {
        // paragraph styles like AlignmentSpan need to end with a new line!
        if (paragraphStyle) {
            output.append("\n");
            len++;
        }
        output.setSpan(repl, where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    if (HtmlTextView.DEBUG) {
        Log.d(HtmlTextView.TAG, "where: " + where);
        Log.d(HtmlTextView.TAG, "len: " + len);
    }
}
 
Example 5
Source File: RichTextWatcher.java    From RichEditor with MIT License 6 votes vote down vote up
/**
 * 删除字符的时候,要删除当前位置start和end相等的span
 */
private void handleDelete() {
    Editable editable = mEditText.getEditableText();
    int cursorPos = mEditText.getSelectionStart();

    ParcelableSpan[] parcelableSpans = editable.getSpans(cursorPos, cursorPos, ParcelableSpan.class);

    for (ParcelableSpan span : parcelableSpans) {
        if (editable.getSpanStart(span) == editable.getSpanEnd(span)) {
            editable.removeSpan(span);
        }
    }

    if (isDeleteEnterStr) {
        // 删除了回车符,如果回车前后两行只要有一行是block样式,就要合并
        mEditText.getRichUtils().mergeBlockSpanAfterDeleteEnter();
    }
}
 
Example 6
Source File: MentionsEditText.java    From Spyglass with Apache License 2.0 5 votes vote down vote up
/**
 * Removes any {@link com.linkedin.android.spyglass.ui.MentionsEditText.DeleteSpan}s and the text within them from
 * the given text.
 *
 * @param text the editable containing DeleteSpans to remove
 */
private void removeTextWithinDeleteSpans(@NonNull Editable text) {
    DeleteSpan[] deleteSpans = text.getSpans(0, text.length(), DeleteSpan.class);
    for (DeleteSpan span : deleteSpans) {
        int spanStart = text.getSpanStart(span);
        int spanEnd = text.getSpanEnd(span);
        text.replace(spanStart, spanEnd, "");
        text.removeSpan(span);
    }
}
 
Example 7
Source File: HiHtmlTagHandler.java    From hipda with GNU General Public License v2.0 5 votes vote down vote up
private void processStrike(boolean opening, Editable output) {
    int len = output.length();
    if (opening) {
        output.setSpan(new StrikethroughSpan(), len, len, Spannable.SPAN_MARK_MARK);
    } else {
        Object obj = getLast(output, StrikethroughSpan.class);
        int where = output.getSpanStart(obj);

        output.removeSpan(obj);

        if (where != len) {
            output.setSpan(new StrikethroughSpan(), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
 
Example 8
Source File: HtmlTagHandler.java    From html-textview with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the text contained within a span and deletes it from the output string
 */
private CharSequence extractSpanText(Editable output, Class kind) {
    final Object obj = getLast(output, kind);
    // start of the tag
    final int where = output.getSpanStart(obj);
    // end of the tag
    final int len = output.length();

    final CharSequence extractedSpanText = output.subSequence(where, len);
    output.delete(where, len);
    return extractedSpanText;
}
 
Example 9
Source File: MentionsEditText.java    From Spyglass with Apache License 2.0 5 votes vote down vote up
/**
 * Resets the given {@link MentionSpan} in the editor, forcing it to redraw with its latest drawable state.
 *
 * @param span the {@link MentionSpan} to update
 */
public void updateSpan(@NonNull MentionSpan span) {
    mBlockCompletion = true;
    Editable text = getText();
    int start = text.getSpanStart(span);
    int end = text.getSpanEnd(span);
    if (start >= 0 && end > start && end <= text.length()) {
        text.removeSpan(span);
        text.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    mBlockCompletion = false;
}
 
Example 10
Source File: EditTextCaption.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private void applyTextStyleToSelection(TypefaceSpan span) {
    int start;
    int end;
    if (selectionStart >= 0 && selectionEnd >= 0) {
        start = selectionStart;
        end = selectionEnd;
        selectionStart = selectionEnd = -1;
    } else {
        start = getSelectionStart();
        end = getSelectionEnd();
    }
    Editable editable = getText();

    CharacterStyle spans[] = editable.getSpans(start, end, CharacterStyle.class);
    if (spans != null && spans.length > 0) {
        for (int a = 0; a < spans.length; a++) {
            CharacterStyle oldSpan = spans[a];
            int spanStart = editable.getSpanStart(oldSpan);
            int spanEnd = editable.getSpanEnd(oldSpan);
            editable.removeSpan(oldSpan);
            if (spanStart < start) {
                editable.setSpan(oldSpan, spanStart, start, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (spanEnd > end) {
                editable.setSpan(oldSpan, end, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
    if (span != null) {
        editable.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    if (delegate != null) {
        delegate.onSpansChanged();
    }
}
 
Example 11
Source File: RichUtils.java    From RichEditor with MIT License 5 votes vote down vote up
/**
 * 处理删除按键
 * 1、删除BlockImageSpan的时候,直接将光标定位到上一行末尾
 * 2、当光标处于BlockImageSpan下一行的第一个位置(不是EditText最后一个字符)上按删除按键时,
 * 不删除字符,而是将光标定位到上一行的末尾(即BlockImageSpan的末尾)
 * 3、当光标处于BlockImageSpan的行首按删除按键时,如果上一行不是空行,则不删除字符,而是将光标移动到上一行的末尾
 */
private boolean handleDeleteKey() {
    Editable editable = mRichEditText.getEditableText();
    int cursorPos = mRichEditText.getSelectionStart();
    if (cursorPos == 0) {
        return false;
    }

    //删除imageSpan的时候直接将光标定位到上一行末尾
    BlockImageSpan[] imageSpans = editable.getSpans(cursorPos - 1, cursorPos, BlockImageSpan.class);
    if (imageSpans.length > 0) {
        ImageSpan imageSpan = imageSpans[0];
        int start = editable.getSpanStart(imageSpan);
        int end = editable.getSpanEnd(imageSpan);
        if (start > 0) {
            start--; //光标跳到上一行
        }
        editable.delete(start, end);
        return true;
    }

    //当光标处于imageSpan下一行(当行不是空行)的第一个位置上按删除按键时
    BlockImageSpan[] imageSpans1 = editable.getSpans(cursorPos - 2, cursorPos - 1, BlockImageSpan.class);
    String content = mRichEditText.getEditableText().toString();
    if (imageSpans1.length > 0 && cursorPos < content.length() && content.charAt(cursorPos) != '\n') {
        mRichEditText.setSelection(cursorPos - 1);
        return true;
    }

    // 当光标处于BlockImageSpan的行首按删除按键时,如果上一行不是空行,则不删除字符,而是将光标移动到上一行的末尾
    BlockImageSpan[] imageSpans2 = editable.getSpans(cursorPos, cursorPos + 1, BlockImageSpan.class);
    if (imageSpans2.length > 0 && cursorPos >= 2
            && content.charAt(cursorPos - 1) == '\n' && content.charAt(cursorPos - 2) != '\n') {
        mRichEditText.setSelection(cursorPos - 1);
        return true;
    }

    return false;
}
 
Example 12
Source File: TokenCompleteTextView.java    From SocialTokenAutoComplete with Apache License 2.0 5 votes vote down vote up
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    System.out.println("changing text: " + s);
    Editable text = getText();
    if (text == null)
        return;

    clearSelections();
    updateHint();

    TokenImageSpan[] spans = text.getSpans(start - before, start - before + count, TokenImageSpan.class);

    for (TokenImageSpan token: spans) {

        int position = start + count;
        if (text.getSpanStart(token) < position && position <= text.getSpanEnd(token)) {
            //We may have to manually reverse the auto-complete and remove the extra ,'s
            int spanStart = text.getSpanStart(token);
            int spanEnd = text.getSpanEnd(token);

            removeToken(token, text);

            //The end of the span is the character index after it
            spanEnd--;

            if (spanEnd >= 0 && text.charAt(spanEnd) == SPLITOR) {
                text.delete(spanEnd, spanEnd + 1);
            }

            if (spanStart > 0 && text.charAt(spanStart) == SPLITOR) {
                text.delete(spanStart, spanStart + 1);
            }
        }
    }
}
 
Example 13
Source File: RichUtils.java    From RichEditor with MIT License 5 votes vote down vote up
/**
 * 处理行内样式各个按钮的状态(点亮或置灰)
 *
 * @param type 样式类型
 */
private boolean handleInlineStyleButtonStatus(@RichTypeEnum String type) {
    Editable editable = mRichEditText.getEditableText();
    int cursorPos = mRichEditText.getSelectionEnd();
    IInlineSpan[] inlineSpans = (IInlineSpan[]) editable.getSpans(cursorPos, cursorPos, getSpanClassFromType(type));
    if (inlineSpans.length <= 0) {
        return false;
    }

    boolean isLight = false; //是否点亮

    for (IInlineSpan span : inlineSpans) {
        int spanStart = editable.getSpanStart(span);
        int spanEnd = editable.getSpanEnd(span);
        int spanFlag = editable.getSpanFlags(span);
        if (spanStart < cursorPos && spanEnd > cursorPos) {
            isLight = true;
        } else if (spanStart == cursorPos
                && (spanFlag == Spanned.SPAN_INCLUSIVE_INCLUSIVE || spanFlag == Spanned.SPAN_INCLUSIVE_EXCLUSIVE)) {
            isLight = true;
        } else if (spanEnd == cursorPos
                && (spanFlag == Spanned.SPAN_INCLUSIVE_INCLUSIVE || spanFlag == Spanned.SPAN_EXCLUSIVE_INCLUSIVE)) {
            isLight = true;
        }
    }

    return isLight;
}
 
Example 14
Source File: RichUtils.java    From RichEditor with MIT License 5 votes vote down vote up
/**
 * 删除指定的段落ImageSpan(如已插入的图片、视频封面、自定义view等)
 * 场景:用户长按ImageSpan触发弹窗然后点击删除操作
 *
 * @param blockImageSpan 段落ImageSpan
 */
public void removeBlockImageSpan(BlockImageSpan blockImageSpan) {
    Editable editable = mRichEditText.getEditableText();
    BlockImageSpan[] blockImageSpans = editable.getSpans(0, editable.length(), BlockImageSpan.class);

    for (BlockImageSpan curBlockImageSpan : blockImageSpans) {
        if (curBlockImageSpan == blockImageSpan) {
            int start = editable.getSpanStart(curBlockImageSpan);
            int end = editable.getSpanEnd(curBlockImageSpan);
            editable.removeSpan(curBlockImageSpan);
            editable.delete(start, end);
            break;
        }
    }
}
 
Example 15
Source File: EditTextCaption.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private void applyTextStyleToSelection(TypefaceSpan span) {
    int start;
    int end;
    if (selectionStart >= 0 && selectionEnd >= 0) {
        start = selectionStart;
        end = selectionEnd;
        selectionStart = selectionEnd = -1;
    } else {
        start = getSelectionStart();
        end = getSelectionEnd();
    }
    Editable editable = getText();

    CharacterStyle spans[] = editable.getSpans(start, end, CharacterStyle.class);
    if (spans != null && spans.length > 0) {
        for (int a = 0; a < spans.length; a++) {
            CharacterStyle oldSpan = spans[a];
            int spanStart = editable.getSpanStart(oldSpan);
            int spanEnd = editable.getSpanEnd(oldSpan);
            editable.removeSpan(oldSpan);
            if (spanStart < start) {
                editable.setSpan(oldSpan, spanStart, start, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (spanEnd > end) {
                editable.setSpan(oldSpan, end, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
    if (span != null) {
        editable.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    if (delegate != null) {
        delegate.onSpansChanged();
    }
}
 
Example 16
Source File: FormattedEditText.java    From FormatEditText with MIT License 4 votes vote down vote up
private int clearPlaceholders(Editable editable, int start) {
    IPlaceholderSpan[] spans;
    boolean sorted;
    if (start > 0) {
        sorted = true;
        mComparator.mEditable = editable;
        IPlaceholderSpan[] left;
        if (mMode < MODE_MASK) {
            int i;
            for (i = start; i > 0; i--) {
                char holder = findPlaceholder(i);
                if (holder == 0) {
                    break;
                }
            }
            start = i;
            left = EMPTY_SPANS;
        } else {
            left = editable.getSpans(0, start, IPlaceholderSpan.class);
            Arrays.sort(left, mComparator);
        }
        IPlaceholderSpan[] right;
        if (start >= editable.length()) {
            right = EMPTY_SPANS;
        } else {
            right = editable.getSpans(start, editable.length(), IPlaceholderSpan.class);
            Arrays.sort(right, mComparator);
        }
        mComparator.mEditable = null;
        if (left.length == 0) {
            spans = right;
        } else if (left.length == start) {
            start = 0;
            spans = new IPlaceholderSpan[left.length + right.length];
            System.arraycopy(left, 0, spans, 0, left.length);
            System.arraycopy(right, 0, spans, left.length, right.length);
        } else {
            int last = start - 1;
            int index;
            for (index = left.length - 1; index >= 0; index--) {
                int spanStart = editable.getSpanStart(left[index]);
                if (last != spanStart) {
                    last += 1;
                    index += 1;
                    break;
                }
                last = spanStart - 1;
            }
            start = last;
            int leftLength = left.length - index;
            if (leftLength == 0) {
                spans = right;
            } else {
                spans = new IPlaceholderSpan[leftLength + right.length];
                System.arraycopy(left, index, spans, 0, leftLength);
                System.arraycopy(right, 0, spans, leftLength, right.length);
            }
        }
    } else {
        sorted = false;
        spans = editable.getSpans(0, editable.length(), IPlaceholderSpan.class);
    }
    if (spans.length == editable.length() - start) {
        editable.delete(start, editable.length());
        if (start == 0 && isNeedClearText()) {
            return -1;
        }
    } else if (spans.length > 0) {
        clearNonEmptySpans(editable, spans, sorted);
    }
    return start;
}
 
Example 17
Source File: UrlBar.java    From AndroidChromium with Apache License 2.0 4 votes vote down vote up
@Override
public boolean commitText(CharSequence text, int newCursorPosition) {
    Editable currentText = getText();
    if (currentText == null) return super.commitText(text, newCursorPosition);

    int selectionStart = Selection.getSelectionStart(currentText);
    int selectionEnd = Selection.getSelectionEnd(currentText);
    int autocompleteIndex = currentText.getSpanStart(mAutocompleteSpan);
    // If the text being committed is a single character that matches the next character
    // in the selection (assumed to be the autocomplete text), we only move the text
    // selection instead clearing the autocomplete text causing flickering as the
    // autocomplete text will appear once the next suggestions are received.
    //
    // To be confident that the selection is an autocomplete, we ensure the selection
    // is at least one character and the end of the selection is the end of the
    // currently entered text.
    if (newCursorPosition == 1 && selectionStart > 0 && selectionStart != selectionEnd
            && selectionEnd >= currentText.length()
            && autocompleteIndex == selectionStart
            && text.length() == 1) {
        currentText.getChars(selectionStart, selectionStart + 1, mTempSelectionChar, 0);
        if (mTempSelectionChar[0] == text.charAt(0)) {

            // Since the text isn't changing, TalkBack won't read out the typed characters.
            // To work around this, explicitly send an accessibility event. crbug.com/416595
            if (mAccessibilityManager != null && mAccessibilityManager.isEnabled()) {
                AccessibilityEvent event = AccessibilityEvent.obtain(
                        AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED);
                event.setFromIndex(selectionStart);
                event.setRemovedCount(0);
                event.setAddedCount(1);
                event.setBeforeText(currentText.toString().substring(0, selectionStart));
                sendAccessibilityEventUnchecked(event);
            }

            setAutocompleteText(
                    currentText.subSequence(0, selectionStart + 1),
                    currentText.subSequence(selectionStart + 1, selectionEnd));
            if (!mInBatchEditMode) {
                notifyAutocompleteTextStateChanged(false);
            }
            return true;
        }
    }

    boolean retVal = super.commitText(text, newCursorPosition);

    // Ensure the autocomplete span is removed if it is no longer valid after committing the
    // text.
    if (getText().getSpanStart(mAutocompleteSpan) >= 0) clearAutocompleteSpanIfInvalid();

    return retVal;
}
 
Example 18
Source File: SpellChecker.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private void createMisspelledSuggestionSpan(Editable editable, SuggestionsInfo suggestionsInfo,
        SpellCheckSpan spellCheckSpan, int offset, int length) {
    final int spellCheckSpanStart = editable.getSpanStart(spellCheckSpan);
    final int spellCheckSpanEnd = editable.getSpanEnd(spellCheckSpan);
    if (spellCheckSpanStart < 0 || spellCheckSpanEnd <= spellCheckSpanStart)
        return; // span was removed in the meantime

    final int start;
    final int end;
    if (offset != USE_SPAN_RANGE && length != USE_SPAN_RANGE) {
        start = spellCheckSpanStart + offset;
        end = start + length;
    } else {
        start = spellCheckSpanStart;
        end = spellCheckSpanEnd;
    }

    final int suggestionsCount = suggestionsInfo.getSuggestionsCount();
    String[] suggestions;
    if (suggestionsCount > 0) {
        suggestions = new String[suggestionsCount];
        for (int i = 0; i < suggestionsCount; i++) {
            suggestions[i] = suggestionsInfo.getSuggestionAt(i);
        }
    } else {
        suggestions = ArrayUtils.emptyArray(String.class);
    }

    SuggestionSpan suggestionSpan = new SuggestionSpan(mTextView.getContext(), suggestions,
            SuggestionSpan.FLAG_EASY_CORRECT | SuggestionSpan.FLAG_MISSPELLED);
    // TODO: Remove mIsSentenceSpellCheckSupported by extracting an interface
    // to share the logic of word level spell checker and sentence level spell checker
    if (mIsSentenceSpellCheckSupported) {
        final Long key = Long.valueOf(TextUtils.packRangeInLong(start, end));
        final SuggestionSpan tempSuggestionSpan = mSuggestionSpanCache.get(key);
        if (tempSuggestionSpan != null) {
            if (DBG) {
                Log.i(TAG, "Cached span on the same position is cleard. "
                        + editable.subSequence(start, end));
            }
            editable.removeSpan(tempSuggestionSpan);
        }
        mSuggestionSpanCache.put(key, suggestionSpan);
    }
    editable.setSpan(suggestionSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    mTextView.invalidateRegion(start, end, false /* No cursor involved */);
}
 
Example 19
Source File: TokenCompleteTextView.java    From SocialTokenAutoComplete with Apache License 2.0 4 votes vote down vote up
private void updateHint() {
    Editable text = getText();
    CharSequence hintText = getHint();
    if (text == null || hintText == null) {
        return;
    }

    //Show hint if we need to
    if (prefix.length() > 0) {
        HintSpan[] hints = text.getSpans(0, text.length(), HintSpan.class);
        HintSpan hint = null;
        int testLength = prefix.length();
        if (hints.length > 0) {
            hint = hints[0];
            testLength += text.getSpanEnd(hint) - text.getSpanStart(hint);
        }

        if (text.length() == testLength) {
            hintVisible = true;

            if (hint != null) {
                return;//hint already visible
            }

            //We need to display the hint manually
            Typeface tf = getTypeface();
            int style = Typeface.NORMAL;
            if (tf != null) {
                style = tf.getStyle();
            }
            ColorStateList colors = getHintTextColors();

            HintSpan hintSpan = new HintSpan(null, style, (int)getTextSize(), colors, colors);
            text.insert(prefix.length(), hintText);
            text.setSpan(hintSpan, prefix.length(), prefix.length() + getHint().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            setSelection(prefix.length());

        } else {
            if (hint == null) {
                return; //hint already removed
            }

            //Remove the hint. There should only ever be one
            int sStart = text.getSpanStart(hint);
            int sEnd = text.getSpanEnd(hint);

            text.removeSpan(hint);
            text.replace(sStart, sEnd, "");

            hintVisible = false;
        }
    }
}
 
Example 20
Source File: UrlBar.java    From delion with Apache License 2.0 4 votes vote down vote up
@Override
public boolean commitText(CharSequence text, int newCursorPosition) {
    Editable currentText = getText();
    if (currentText == null) return super.commitText(text, newCursorPosition);

    int selectionStart = Selection.getSelectionStart(currentText);
    int selectionEnd = Selection.getSelectionEnd(currentText);
    int autocompleteIndex = currentText.getSpanStart(mAutocompleteSpan);
    // If the text being committed is a single character that matches the next character
    // in the selection (assumed to be the autocomplete text), we only move the text
    // selection instead clearing the autocomplete text causing flickering as the
    // autocomplete text will appear once the next suggestions are received.
    //
    // To be confident that the selection is an autocomplete, we ensure the selection
    // is at least one character and the end of the selection is the end of the
    // currently entered text.
    if (newCursorPosition == 1 && selectionStart > 0 && selectionStart != selectionEnd
            && selectionEnd >= currentText.length()
            && autocompleteIndex == selectionStart
            && text.length() == 1) {
        currentText.getChars(selectionStart, selectionStart + 1, mTempSelectionChar, 0);
        if (mTempSelectionChar[0] == text.charAt(0)) {

            // Since the text isn't changing, TalkBack won't read out the typed characters.
            // To work around this, explicitly send an accessibility event. crbug.com/416595
            if (mAccessibilityManager != null && mAccessibilityManager.isEnabled()) {
                AccessibilityEvent event = AccessibilityEvent.obtain(
                        AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED);
                event.setFromIndex(selectionStart);
                event.setRemovedCount(0);
                event.setAddedCount(1);
                event.setBeforeText(currentText.toString().substring(0, selectionStart));
                sendAccessibilityEventUnchecked(event);
            }

            setAutocompleteText(
                    currentText.subSequence(0, selectionStart + 1),
                    currentText.subSequence(selectionStart + 1, selectionEnd));
            if (!mInBatchEditMode) {
                notifyAutocompleteTextStateChanged(false);
            }
            return true;
        }
    }

    boolean retVal = super.commitText(text, newCursorPosition);

    // Ensure the autocomplete span is removed if it is no longer valid after committing the
    // text.
    if (getText().getSpanStart(mAutocompleteSpan) >= 0) clearAutocompleteSpanIfInvalid();

    return retVal;
}