Example usage for android.text.method QwertyKeyListener markAsReplaced

List of usage examples for android.text.method QwertyKeyListener markAsReplaced

Introduction

In this page you can find the example usage for android.text.method QwertyKeyListener markAsReplaced.

Prototype

public static void markAsReplaced(Spannable content, int start, int end, String original) 

Source Link

Document

Marks the specified region of content as having contained original prior to AutoText replacement.

Usage

From source file:com.duy.pascal.ui.editor.view.CodeSuggestsEditText.java

protected void replaceText(CharSequence text) {
    clearComposingText();/*from  w w  w.j  a v a2s .c  om*/

    int end = getSelectionEnd();
    int start;
    if (SymbolsTokenizer.TOKEN.contains(text.toString().trim())) {
        start = end;
    } else {
        start = mTokenizer.findTokenStart(getText(), end);
    }
    start = Math.max(start, 0);
    end = Math.max(end, 0);
    Editable editable = getText();
    String original = TextUtils.substring(editable, start, end);

    QwertyKeyListener.markAsReplaced(editable, start, end, original);
    editable.replace(start, end, mTokenizer.terminateToken(text));
}

From source file:com.android.ex.chips.RecipientEditTextView.java

private boolean commitChip(final int start, final int end, final Editable editable) {
    final ListAdapter adapter = getAdapter();
    if (adapter != null && adapter.getCount() > 0 && enoughToFilter() && end == getSelectionEnd()
            && !isPhoneQuery()) {
        // choose the first entry.
        submitItemAtPosition(0);//  w  w w. ja  v a  2s  .c om
        dismissDropDown();
        return true;
    } else {
        int tokenEnd = mTokenizer.findTokenEnd(editable, start);
        if (editable.length() > tokenEnd + 1) {
            final char charAt = editable.charAt(tokenEnd + 1);
            if (charAt == COMMIT_CHAR_COMMA || charAt == COMMIT_CHAR_SEMICOLON || charAt == COMMIT_CHAR_SPACE) //// ---- Added by shreyash
                tokenEnd++;

        }

        //-----------------------
        final String text = editable.toString().substring(start, tokenEnd).trim();
        clearComposingText();
        if (text != null && text.length() > 0 && !text.equals(" ")) {
            final RecipientEntry entry = createTokenizedEntry(text);
            if (entry != null) {
                QwertyKeyListener.markAsReplaced(editable, start, end, "");
                final CharSequence chipText = createChip(entry, false);
                if (chipText != null && start > -1 && end > -1)
                    editable.replace(start, end, chipText);
            }
            // Only dismiss the dropdown if it is related to the text we
            // just committed.
            // For paste, it may not be as there are possibly multiple
            // tokens being added.
            if (end == getSelectionEnd())
                dismissDropDown();
            sanitizeBetween();
            return true;
        }
    }
    return false;
}

From source file:com.android.ex.chips.RecipientEditTextView.java

private void handleEdit(final int start, final int end) {
    if (start == -1 || end == -1) {
        // This chip no longer exists in the field.
        dismissDropDown();/*from w  ww .ja v  a  2s  .c  om*/
        return;
    }
    // This is in the middle of a chip, so select out the whole chip
    // and commit it.
    final Editable editable = getText();
    setSelection(end);
    final String text = getText().toString().substring(start, end);
    if (!TextUtils.isEmpty(text)) {
        final RecipientEntry entry = RecipientEntry.constructFakeEntry(text, isValid(text));
        QwertyKeyListener.markAsReplaced(editable, start, end, "");
        final CharSequence chipText = createChip(entry, false);
        final int selEnd = getSelectionEnd();
        if (chipText != null && start > -1 && selEnd > -1)
            editable.replace(start, selEnd, chipText);
    }
    dismissDropDown();
}

From source file:com.android.ex.chips.RecipientEditTextView.java

private void submitItemAtPosition(final int position) {
    final RecipientEntry entry = createValidatedEntry(getAdapter().getItem(position));
    if (entry == null)
        return;/*www  .j a v  a 2s .c  o m*/
    clearComposingText();
    final int end = getSelectionEnd();
    final int start = mTokenizer.findTokenStart(getText(), end);
    final Editable editable = getText();
    QwertyKeyListener.markAsReplaced(editable, start, end, "");
    final CharSequence chip = createChip(entry, false);
    if (chip != null && start >= 0 && end >= 0)
        editable.replace(start, end, chip);
    sanitizeBetween();
    if (mChipListener != null)
        mChipListener.onDataChanged();
}

From source file:com.android.ex.chips.RecipientEditTextView.java

/**
 * Show specified chip as selected. If the RecipientChip is just an email address, selecting the chip will take the
 * contents of the chip and place it at the end of the RecipientEditTextView for inline editing. If the
 * RecipientChip is a complete contact, then selecting the chip will change the background color of the chip, show
 * the delete icon, and a popup window with the address in use highlighted and any other alternate addresses for the
 * contact.//from  w w  w  .  j a  va 2s. com
 *
 * @param currentChip
 * Chip to select.
 * @return A RecipientChip in the selected state or null if the chip just contained an email address.
 */
private DrawableRecipientChip selectChip(final DrawableRecipientChip currentChip) {
    if (shouldShowEditableText(currentChip)) {
        final CharSequence text = currentChip.getValue();
        final Editable editable = getText();
        final Spannable spannable = getSpannable();
        final int spanStart = spannable.getSpanStart(currentChip);
        final int spanEnd = spannable.getSpanEnd(currentChip);
        spannable.removeSpan(currentChip);
        editable.delete(spanStart, spanEnd);
        setCursorVisible(true);
        setSelection(editable.length());
        editable.append(text);
        return constructChipSpan(RecipientEntry.constructFakeEntry((String) text, isValid(text.toString())),
                true, false);
    } else if (currentChip.getContactId() == RecipientEntry.GENERATED_CONTACT || currentChip.isGalContact()) {
        final int start = getChipStart(currentChip);
        final int end = getChipEnd(currentChip);
        getSpannable().removeSpan(currentChip);
        DrawableRecipientChip newChip;
        try {
            if (mNoChips)
                return null;
            newChip = constructChipSpan(currentChip.getEntry(), true, false);
        } catch (final NullPointerException e) {
            Log.e(TAG, e.getMessage(), e);
            return null;
        }
        final Editable editable = getText();
        QwertyKeyListener.markAsReplaced(editable, start, end, "");
        if (start == -1 || end == -1)
            Log.d(TAG, "The chip being selected no longer exists but should.");
        else
            editable.setSpan(newChip, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        newChip.setSelected(true);
        if (shouldShowEditableText(newChip))
            scrollLineIntoView(getLayout().getLineForOffset(getChipStart(newChip)));
        //showAddress(newChip,mAddressPopup,getWidth());
        setCursorVisible(false);
        return newChip;
    } else {
        final int start = getChipStart(currentChip);
        final int end = getChipEnd(currentChip);
        getSpannable().removeSpan(currentChip);
        DrawableRecipientChip newChip;
        try {
            newChip = constructChipSpan(currentChip.getEntry(), true, false);
        } catch (final NullPointerException e) {
            Log.e(TAG, e.getMessage(), e);
            return null;
        }
        final Editable editable = getText();
        QwertyKeyListener.markAsReplaced(editable, start, end, "");
        if (start == -1 || end == -1)
            Log.d(TAG, "The chip being selected no longer exists but should.");
        else
            editable.setSpan(newChip, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        newChip.setSelected(true);
        if (shouldShowEditableText(newChip))
            scrollLineIntoView(getLayout().getLineForOffset(getChipStart(newChip)));
        //showAlternates(newChip,mAlternatesPopup,getWidth());
        setCursorVisible(false);
        return newChip;
    }
}

From source file:com.android.ex.chips.RecipientEditTextView.java

/**
 * Remove selection from this chip. Unselecting a RecipientChip will render the chip without a delete icon and with
 * an unfocused background. This is called when the RecipientChip no longer has focus.
 *//*w w  w .j a  v  a 2 s  . com*/
private void unselectChip(final DrawableRecipientChip chip) {
    final int start = getChipStart(chip);
    final int end = getChipEnd(chip);
    final Editable editable = getText();
    mSelectedChip = null;
    if (start == -1 || end == -1) {
        Log.w(TAG, "The chip doesn't exist or may be a chip a user was editing");
        setSelection(editable.length());
        commitDefault();
    } else {
        getSpannable().removeSpan(chip);
        QwertyKeyListener.markAsReplaced(editable, start, end, "");
        editable.removeSpan(chip);
        try {
            if (!mNoChips)
                editable.setSpan(constructChipSpan(chip.getEntry(), false, false), start, end,
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        } catch (final NullPointerException e) {
            Log.e(TAG, e.getMessage(), e);
        }
    }
    setCursorVisible(true);
    setSelection(editable.length());
    /*if(mAlternatesPopup!=null&&mAlternatesPopup.isShowing())
      mAlternatesPopup.dismiss();*/
}