Java Code Examples for android.widget.EditText#getEditableText()

The following examples show how to use android.widget.EditText#getEditableText() . 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: SourceEditActivity.java    From MyBookshelf with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void sendText(@NotNull String txt) {
    if (isEmpty(txt)) return;
    View view = getWindow().getDecorView().findFocus();
    if (view instanceof EditText) {
        EditText editText = (EditText) view;
        int start = editText.getSelectionStart();
        int end = editText.getSelectionEnd();
        Editable edit = editText.getEditableText();//获取EditText的文字
        if (start < 0 || start >= edit.length()) {
            edit.append(txt);
        } else {
            edit.replace(start, end, txt);//光标所在位置插入文字
        }
    }
}
 
Example 2
Source File: SourceEditActivity.java    From a with GNU General Public License v3.0 5 votes vote down vote up
private void insertTextToEditText(String txt) {
    if (isEmpty(txt)) return;
    View view = getWindow().getDecorView().findFocus();
    if (view instanceof EditText) {
        EditText editText = (EditText) view;
        int start = editText.getSelectionStart();
        int end = editText.getSelectionEnd();
        Editable edit = editText.getEditableText();//获取EditText的文字
        if (start < 0 || start >= edit.length()) {
            edit.append(txt);
        } else {
            edit.replace(start, end, txt);//光标所在位置插入文字
        }
    }
}
 
Example 3
Source File: ChipDrawableSpan.java    From lttrs-android with Apache License 2.0 5 votes vote down vote up
public static void reset(EditText editText) {
    final Editable editable = editText.getEditableText();
    if (TextUtils.isEmpty(editable)) {
        return;
    }
    editable.clearSpans();
    ChipDrawableSpan.apply(editText.getContext(), editable, editText.hasFocus());
}
 
Example 4
Source File: SourceEditActivity.java    From HaoReader with GNU General Public License v3.0 5 votes vote down vote up
private void insertTextToEditText(String txt) {
    if (TextUtils.isEmpty(txt)) return;
    View view = getWindow().getDecorView().findFocus();
    if (view instanceof EditText) {
        EditText editText = (EditText) view;
        int start = editText.getSelectionStart();
        int end = editText.getSelectionEnd();
        Editable edit = editText.getEditableText();//获取EditText的文字
        if (start < 0 || start >= edit.length()) {
            edit.append(txt);
        } else {
            edit.replace(start, end, txt);//光标所在位置插入文字
        }
    }
}
 
Example 5
Source File: SpanTextHelper.java    From YCCustomText with Apache License 2.0 5 votes vote down vote up
/**
 * 修改加粗样式
 */
public void bold(EditText lastFocusEdit) {
    //获取editable对象
    Editable editable = lastFocusEdit.getEditableText();
    //获取当前选中的起始位置
    int start = lastFocusEdit.getSelectionStart();
    //获取当前选中的末尾位置
    int end = lastFocusEdit.getSelectionEnd();
    HyperLogUtils.i("bold select  Start:" + start + "   end:  " + end);
    if (checkNormalStyle(start, end)) {
        return;
    }
    new BoldStyle().applyStyle(editable, start, end);
}
 
Example 6
Source File: SpanTextHelper.java    From YCCustomText with Apache License 2.0 5 votes vote down vote up
/**
 * 修改斜体样式
 */
public void italic(EditText lastFocusEdit) {
    Editable editable = lastFocusEdit.getEditableText();
    int start = lastFocusEdit.getSelectionStart();
    int end = lastFocusEdit.getSelectionEnd();
    HyperLogUtils.i("italic select  Start:" + start + "   end:  " + end);
    if (checkNormalStyle(start, end)) {
        return;
    }
    new ItalicStyle().applyStyle(editable, start, end);
}
 
Example 7
Source File: SpanTextHelper.java    From YCCustomText with Apache License 2.0 5 votes vote down vote up
/**
 * 修改加粗斜体样式
 */
public void boldItalic(EditText lastFocusEdit) {
    Editable editable = lastFocusEdit.getEditableText();
    int start = lastFocusEdit.getSelectionStart();
    int end = lastFocusEdit.getSelectionEnd();
    HyperLogUtils.i("boldItalic select  Start:" + start + "   end:  " + end);
    if (checkNormalStyle(start, end)) {
        return;
    }
    new BoldItalicStyle().applyStyle(editable, start, end);
}
 
Example 8
Source File: SpanTextHelper.java    From YCCustomText with Apache License 2.0 5 votes vote down vote up
/**
 * 修改删除线样式
 */
public void strikeThrough(EditText lastFocusEdit) {
    Editable editable = lastFocusEdit.getEditableText();
    int start = lastFocusEdit.getSelectionStart();
    int end = lastFocusEdit.getSelectionEnd();
    HyperLogUtils.i("strikeThrough select  Start:" + start + "   end:  " + end);
    if (checkNormalStyle(start, end)) {
        return;
    }
    new StrikeThroughStyle().applyStyle(editable, start, end);
}
 
Example 9
Source File: SpanTextHelper.java    From YCCustomText with Apache License 2.0 5 votes vote down vote up
/**
 * 修改下划线样式
 */
public void underline(EditText lastFocusEdit) {
    Editable editable = lastFocusEdit.getEditableText();
    int start = lastFocusEdit.getSelectionStart();
    int end = lastFocusEdit.getSelectionEnd();
    HyperLogUtils.i("underline select  Start:" + start + "   end:  " + end);
    if (checkNormalStyle(start, end)) {
        return;
    }
    new UnderlineStyle().applyStyle(editable, start, end);
}
 
Example 10
Source File: InputMethodUtils.java    From Simpler with Apache License 2.0 4 votes vote down vote up
/**
 * 设置输入框的光标到末尾
 */
public static void setEditTextSelectionToEnd(EditText editText) {
    Editable editable = editText.getEditableText();
    Selection.setSelection((Spannable) editable, editable.toString().length());
}
 
Example 11
Source File: KeyboardUtil.java    From Android-Architecture with Apache License 2.0 4 votes vote down vote up
/**
 * 设置输入框的光标到末尾
 */
public static void setEditTextSelectionToEnd(EditText editText) {
    Editable editable = editText.getEditableText();
    Selection.setSelection((Spannable) editable,
            editable.toString().length());
}
 
Example 12
Source File: PostFormMarkup.java    From Overchan-Android with GNU General Public License v3.0 4 votes vote down vote up
public static void markup(int markType, EditText commentField, int feature) {
    Editable comment = commentField.getEditableText();
    String text = comment.toString();
    int selectionStart = Math.max(0, commentField.getSelectionStart());
    int selectionEnd = Math.min(text.length(), commentField.getSelectionEnd());
    text = text.substring(selectionStart, selectionEnd);
    
    if (markType == BoardModel.MARK_WAKABAMARK) {
        switch (feature) {
            case FEATURE_BOLD:
                comment.replace(selectionStart, selectionEnd, "**" + text.replace("\n", "**\n**") + "**");
                commentField.setSelection(selectionStart + 2);
                break;
            case FEATURE_ITALIC:
                comment.replace(selectionStart, selectionEnd, "*" + text.replace("\n", "*\n*") + "*");
                commentField.setSelection(selectionStart + 1);
                break;
            case FEATURE_STRIKE:
                StringBuilder strike = new StringBuilder();
                for (String s : text.split("\n")) {
                    strike.append(s);
                    for (int i=0; i<s.length(); ++i) strike.append("^H");
                    strike.append('\n');
                }
                comment.replace(selectionStart, selectionEnd, strike.substring(0, strike.length() - 1));
                commentField.setSelection(selectionStart);
                break;
            case FEATURE_SPOILER:
                comment.replace(selectionStart, selectionEnd, "%%" + text.replace("\n", "%%\n%%") + "%%");
                commentField.setSelection(selectionStart + 2);
                break;
            case FEATURE_QUOTE:
                comment.replace(selectionStart, selectionEnd, ">" + text.replace("\n", "\n>"));
                break;
        }
    } else if (markType == BoardModel.MARK_BBCODE) {
        switch (feature) {
            case FEATURE_BOLD:
                comment.replace(selectionStart, selectionEnd, "[b]" + text + "[/b]");
                commentField.setSelection(selectionStart + 3);
                break;
            case FEATURE_ITALIC:
                comment.replace(selectionStart, selectionEnd, "[i]" + text + "[/i]");
                commentField.setSelection(selectionStart + 3);
                break;
            case FEATURE_UNDERLINE:
                comment.replace(selectionStart, selectionEnd, "[u]" + text + "[/u]");
                commentField.setSelection(selectionStart + 3);
                break;
            case FEATURE_STRIKE:
                comment.replace(selectionStart, selectionEnd, "[s]" + text + "[/s]");
                commentField.setSelection(selectionStart + 3);
                break;
            case FEATURE_SPOILER:
                comment.replace(selectionStart, selectionEnd, "[spoiler]" + text + "[/spoiler]");
                commentField.setSelection(selectionStart + 9);
                break;
            case FEATURE_QUOTE:
                comment.replace(selectionStart, selectionEnd, ">" + text.replace("\n", "\n>"));
                break;
        }
    } else if (markType == BoardModel.MARK_4CHAN) {
        switch (feature) {
            case FEATURE_BOLD:
                comment.replace(selectionStart, selectionEnd, "**" + text.replace("\n", "**\n**") + "**");
                commentField.setSelection(selectionStart + 2);
                break;
            case FEATURE_ITALIC:
                comment.replace(selectionStart, selectionEnd, "*" + text.replace("\n", "*\n*") + "*");
                commentField.setSelection(selectionStart + 1);
                break;
            case FEATURE_UNDERLINE:
                comment.replace(selectionStart, selectionEnd, "__" + text.replace("\n", "__\n__") + "__");
                commentField.setSelection(selectionStart + 2);
                break;
            case FEATURE_SPOILER:
                comment.replace(selectionStart, selectionEnd, "[spoiler]" + text + "[/spoiler]");
                commentField.setSelection(selectionStart + 9);
                break;
            case FEATURE_QUOTE:
                comment.replace(selectionStart, selectionEnd, ">" + text.replace("\n", "\n>"));
                break;
        }
    }
}