Example usage for android.text Editable insert

List of usage examples for android.text Editable insert

Introduction

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

Prototype

public Editable insert(int where, CharSequence text);

Source Link

Document

Convenience for replace(where, where, text, 0, text.length());

Usage

From source file:me.albertonicoletti.latex.activities.EditorActivity.java

/**
 * Used to maintain the same indentation as the upper line
 * @param editable Text/*from w w  w .  ja va  2  s . c  om*/
 * @param spannable Spannable
 * @param span Modified span
 */
private void autoIndentAndTabEditor(Editable editable, SpannableString spannable, RelativeSizeSpan span) {
    int beginIndex = spannable.getSpanStart(span);
    int endIndex = spannable.getSpanEnd(span);
    // If the last written character is a newline
    if (editable.length() > 0) {
        if (editable.charAt(endIndex - 1) == '\n') {
            int lineModified = editor.getLayout().getLineForOffset(beginIndex);
            int modifiedBeginIndex = editor.getLayout().getLineStart(lineModified);
            int modifiedEndIndex = editor.getLayout().getLineEnd(lineModified);
            String str = editable.subSequence(modifiedBeginIndex, modifiedEndIndex).toString();
            // Collects the whitespaces and tabulations in the upper line
            String whitespaces = "";
            int i = 0;
            while (str.charAt(i) == ' ' || str.charAt(i) == '\t') {
                whitespaces += str.charAt(i);
                i++;
            }
            // And inserts them in the newline
            editable.insert(beginIndex + 1, whitespaces);
        }
        if (editable.charAt(endIndex - 1) == '\t') {
            int tabSize = Integer.valueOf(PreferenceManager.getDefaultSharedPreferences(this)
                    .getString(SettingsActivity.TAB_SIZE, ""));
            String whitespaces = "";
            for (int i = 0; i < tabSize; i++) {
                whitespaces += " ";
            }
            editable.replace(beginIndex, beginIndex + 1, whitespaces);
        }
    }
}

From source file:org.zywx.wbpalmstar.plugin.chatkeyboard.ACEChatKeyboardView.java

public void insertTextByKeyword(String keyword, String insertText, String color, boolean isReplaceKeyword) {
    if (TextUtils.isEmpty(keyword) || TextUtils.isEmpty(insertText)) {
        return;/*from   www .  j  av  a  2  s  .  c o m*/
    }
    if (TextUtils.isEmpty(color)) {
        color = "#507daf";
    }
    Editable editable = mEditText.getText();
    int lastAtPosition = mLastAtPosition;
    if (editable.length() < keyword.length() || editable.length() < lastAtPosition + keyword.length()) {
        return;
    }
    if (!keyword.equals(editable.subSequence(lastAtPosition, lastAtPosition + keyword.length()).toString())) {
        if (!editable.toString().contains(keyword)) {
            return;
        }
        lastAtPosition = editable.toString().lastIndexOf(keyword);
    }
    if (isReplaceKeyword) {
        editable.replace(lastAtPosition, lastAtPosition + keyword.length(),
                Html.fromHtml("<font color=\"" + color + "\">" + insertText + "</font>"));
    } else {
        editable.insert(lastAtPosition + keyword.length(),
                Html.fromHtml("<font color=\"" + color + "\">" + insertText + "</font>"));
    }
    lastAtPosition += insertText.length() + (isReplaceKeyword ? 0 : keyword.length());
    mEditText.setSelection(lastAtPosition);
}

From source file:xj.property.activity.HXBaseActivity.ChatActivity.java

/**
 * ?gridview?view//from   w w w .  ja  va 2  s  .  c o  m
 *
 * @param i
 * @return
 */
private View getGridChildView(int i) {
    View view = View.inflate(this, R.layout.expression_gridview, null);
    ExpandGridView gv = (ExpandGridView) view.findViewById(R.id.gridview);
    List<String> list = new ArrayList<String>();
    if (i == 1) {
        List<String> list1 = reslist.subList(0, 20);/// 20/27//34
        list.addAll(list1);
    } else if (i == 2) {
        list.addAll(reslist.subList(20, 40));
    } else if (i == 3) {
        list.addAll(reslist.subList(40, 60));
    } else if (i == 4) {
        list.addAll(reslist.subList(60, 80));
    } else if (i == 5) {
        list.addAll(reslist.subList(80, reslist.size()));
    }
    list.add("delete_expression");
    final ExpressionAdapter expressionAdapter = new ExpressionAdapter(this, 1, list);
    gv.setAdapter(expressionAdapter);
    gv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String filename = expressionAdapter.getItem(position);
            try {
                // ????
                // ?????
                if (buttonSetModeKeyboard.getVisibility() != View.VISIBLE) {

                    if (filename != "delete_expression") { // ?
                        // ????SmileUtils
                        Class clz = Class.forName("xj.property.utils.SmileUtils");

                        Field field = clz.getField(filename);

                        int selectionStart = mEditTextContent.getSelectionStart();// ??
                        Spannable smiledText = SmileUtils.getSmiledText(getmContext(),
                                (String) field.get(null));
                        Editable editableText = mEditTextContent.getEditableText();
                        editableText.insert(selectionStart, smiledText);

                        //                        mEditTextContent.append(SmileUtils.getSmiledText(getmContext(), (String) field.get(null)));

                    } else {
                        // 
                        if (!TextUtils.isEmpty(mEditTextContent.getText())) {

                            int selectionStart = mEditTextContent.getSelectionStart();// ??

                            if (selectionStart > 0) {

                                String body = mEditTextContent.getText().toString();

                                String tempStr = body.substring(0, selectionStart);

                                int i = tempStr.lastIndexOf("[");// ???
                                if (i != -1) {
                                    /// ??
                                    CharSequence cs = tempStr.substring(i, selectionStart);

                                    if (SmileUtils.containsKey(cs.toString()))
                                        /// ???
                                        mEditTextContent.getEditableText().delete(i, selectionStart);
                                    else
                                        mEditTextContent.getEditableText().delete(selectionStart - 1,
                                                selectionStart);
                                } else {

                                    mEditTextContent.getEditableText().delete(selectionStart - 1,
                                            selectionStart);
                                }
                            }
                        }

                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    });
    return view;
}

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

/**
 * Handles pasting a {@link ClipData} to this {@link RecipientEditTextView}.
 *//*w  w  w  . j a  v a  2  s  .  c o m*/
private void handlePasteClip(final ClipData clip) {
    removeTextChangedListener(mTextWatcher);
    if (clip != null && clip.getDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN))
        for (int i = 0; i < clip.getItemCount(); i++) {
            final CharSequence paste = clip.getItemAt(i).getText();
            if (paste != null) {
                final int start = getSelectionStart();
                final int end = getSelectionEnd();
                final Editable editable = getText();
                if (start >= 0 && end >= 0 && start != end)
                    editable.append(paste, start, end);
                else
                    editable.insert(end, paste);
                handlePasteAndReplace();
            }
        }
    mHandler.post(mAddTextWatcher);
}

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

/**
 * Replace this currently selected chip with a new chip that uses the contact data provided.
 *///from  ww w . j av a2s  .co  m
// Visible for testing.
/* package */void replaceChip(final DrawableRecipientChip chip, final RecipientEntry entry) {
    final boolean wasSelected = chip == mSelectedChip;
    if (wasSelected)
        mSelectedChip = null;
    final int start = getChipStart(chip);
    final int end = getChipEnd(chip);
    getSpannable().removeSpan(chip);
    final Editable editable = getText();
    final CharSequence chipText = createChip(entry, false);
    if (chipText != null)
        if (start == -1 || end == -1) {
            Log.e(TAG, "The chip to replace does not exist but should.");
            editable.insert(0, chipText);
        } else if (!TextUtils.isEmpty(chipText)) {
            // There may be a space to replace with this chip's new
            // associated space. Check for it
            int toReplace = end;
            while (toReplace >= 0 && toReplace < editable.length() && editable.charAt(toReplace) == ' ')
                toReplace++;
            editable.replace(start, toReplace, chipText);
        }
    setCursorVisible(true);
    if (wasSelected)
        clearSelectedChip();
    if (mChipListener != null)
        mChipListener.onDataChanged();
}