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

The following examples show how to use android.widget.EditText#getSelectionEnd() . 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: OnlineImgImpl.java    From chaoli-forum-for-android-2 with GNU General Public License v3.0 6 votes vote down vote up
public void setText(String text){
    text = removeNewlineInFormula(text);
    text += '\n';

    SpannableStringBuilder builder = SFXParser3.parse(((View) mView).getContext(), text, mAttachmentList);

    if (mView instanceof EditText) {
        EditText editText = (EditText) mView;
        int selectionStart = editText.getSelectionStart();
        int selectionEnd = editText.getSelectionEnd();
        mView.setText(builder);
        editText.setSelection(selectionStart, selectionEnd);
    } else {
        mView.setText(builder);
    }

    retrieveOnlineImg(builder);
}
 
Example 3
Source File: BaseConverterFragment.java    From text_converter with GNU General Public License v3.0 6 votes vote down vote up
private void backspace() {
    EditText editText = getCurrentEditText();
    if (editText != null) {
        int selectionStart = editText.getSelectionStart();
        int selectionEnd = editText.getSelectionEnd();
        selectionStart = Math.max(0, selectionStart);
        selectionEnd = Math.max(0, selectionEnd);
        if (selectionStart != selectionEnd) {
            editText.getText().replace(selectionStart, selectionEnd, "");
        } else {
            if (selectionEnd > 0) {
                editText.getText().delete(selectionEnd - 1, selectionEnd);
                editText.setSelection(selectionEnd - 1);
            }
        }
    }
}
 
Example 4
Source File: MarkDownProvider.java    From mvvm-template with GNU General Public License v3.0 6 votes vote down vote up
public static void insertAtCursor(@NonNull EditText editText, @NonNull String text) {
    String oriContent = editText.getText().toString();
    int start = editText.getSelectionStart();
    int end = editText.getSelectionEnd();
    Logger.e(start, end);
    if (start >= 0 && end > 0 && start != end) {
        editText.setText(editText.getText().replace(start, end, text));
    } else {
        int index = editText.getSelectionStart() >= 0 ? editText.getSelectionStart() : 0;
        Logger.e(start, end, index);
        StringBuilder builder = new StringBuilder(oriContent);
        builder.insert(index, text);
        editText.setText(builder.toString());
        editText.setSelection(index + text.length());
    }
}
 
Example 5
Source File: ExpressionTransformEngine.java    From android-expression with Apache License 2.0 6 votes vote down vote up
/**
 * 每次点击表情会调用,已经处理长按选择多个表情然后再输入表情的情况
 * @param editText
 * @param str
 */
public static void input(EditText editText, String str) {


    if (editText == null || str == null) {
        return;
    }
    String t = editText.getText().toString();
    int start = editText.getSelectionStart();
    int end = editText.getSelectionEnd();
    if (start < 0) {
        editText.append(str);
    } else {
        editText.getText().replace(Math.min(start, end), Math.max(start, end), str, 0, str.length());
    }
}
 
Example 6
Source File: MarkDownProvider.java    From mvvm-template with GNU General Public License v3.0 5 votes vote down vote up
public static void addItalic(@NonNull EditText editText) {
    String source = editText.getText().toString();
    int selectionStart = editText.getSelectionStart();
    int selectionEnd = editText.getSelectionEnd();
    String substring = source.substring(selectionStart, selectionEnd);
    String result = "_" + substring + "_ ";
    editText.getText().replace(selectionStart, selectionEnd, result);
    editText.setSelection(result.length() + selectionStart - 2);

}
 
Example 7
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 8
Source File: EditorFragment.java    From android-discourse with Apache License 2.0 5 votes vote down vote up
private void markdownCode() {
    final EditText et = mContentET;
    Editable editable = et.getText();
    int start = et.getSelectionStart();
    if (et.hasSelection()) {
        int end = et.getSelectionEnd();
        editable.insert(end, CODE);
        editable.insert(start, CODE);
        et.setSelection(start + 1, end + 1);
    } else {
        editable.insert(start, CODE2);
        et.setSelection(start + 1);
    }
}
 
Example 9
Source File: MarkDownProvider.java    From mvvm-template with GNU General Public License v3.0 5 votes vote down vote up
public static void addStrikeThrough(@NonNull EditText editText) {
    String source = editText.getText().toString();
    int selectionStart = editText.getSelectionStart();
    int selectionEnd = editText.getSelectionEnd();
    String substring = source.substring(selectionStart, selectionEnd);
    String result = "~~" + substring + "~~ ";
    editText.getText().replace(selectionStart, selectionEnd, result);
    editText.setSelection(result.length() + selectionStart - 3);

}
 
Example 10
Source File: MarkDownProvider.java    From mvvm-template with GNU General Public License v3.0 5 votes vote down vote up
public static void addInlinleCode(@NonNull EditText editText) {
    String source = editText.getText().toString();
    int selectionStart = editText.getSelectionStart();
    int selectionEnd = editText.getSelectionEnd();
    String substring = source.substring(selectionStart, selectionEnd);
    String result = "`" + substring + "` ";
    editText.getText().replace(selectionStart, selectionEnd, result);
    editText.setSelection(result.length() + selectionStart - 2);

}
 
Example 11
Source File: MarkDownProvider.java    From mvvm-template with GNU General Public License v3.0 5 votes vote down vote up
public static void addBold(@NonNull EditText editText) {
    String source = editText.getText().toString();
    int selectionStart = editText.getSelectionStart();
    int selectionEnd = editText.getSelectionEnd();
    String substring = source.substring(selectionStart, selectionEnd);
    String result = "**" + substring + "** ";
    editText.getText().replace(selectionStart, selectionEnd, result);
    editText.setSelection(result.length() + selectionStart - 3);

}
 
Example 12
Source File: EditTextSelectionState.java    From quill with MIT License 5 votes vote down vote up
public EditTextSelectionState(@NonNull EditText editText) {
    mEditText = editText;
    mFocused = editText.hasFocus();
    int selectionStart = editText.getSelectionStart();
    int selectionEnd = editText.getSelectionEnd();
    if (selectionStart > selectionEnd && selectionStart != -1 && selectionEnd != -1) {
        mSelectionStart = selectionEnd;
        mSelectionEnd = selectionStart;
    } else {
        mSelectionStart = selectionStart;
        mSelectionEnd = selectionEnd;
    }
}
 
Example 13
Source File: MarkDownProvider.java    From mvvm-template with GNU General Public License v3.0 5 votes vote down vote up
public static void addHeader(@NonNull EditText editText, int level) {
    String source = editText.getText().toString();
    int selectionStart = editText.getSelectionStart();
    int selectionEnd = editText.getSelectionEnd();
    StringBuilder result = new StringBuilder();
    String substring = source.substring(selectionStart, selectionEnd);
    if (!hasNewLine(source, selectionStart))
        result.append("\n");
    IntStream.range(0, level).forEach(integer -> result.append("#"));
    result.append(" ").append(substring);
    editText.getText().replace(selectionStart, selectionEnd, result.toString());
    editText.setSelection(selectionStart + result.length());

}
 
Example 14
Source File: WhatsappViewCompat.java    From WhatsappFormatter with Apache License 2.0 5 votes vote down vote up
/**
 * Performs formatting.
 */
private static void format(EditText editText, TextWatcher mainWatcher, TextWatcher[] otherWatchers ) {

    Editable text = editText.getText();
    CharSequence formatted = WhatsappViewCompat.extractFlagsForEditText(text);
    removeTextChangedListener(editText,mainWatcher);
    int selectionEnd = editText.getSelectionEnd();
    int selectionStart = editText.getSelectionStart();
    editText.setText(formatted);
    editText.setSelection(selectionStart, selectionEnd);
    Editable formattedEditableText = editText.getText();
    sendAfterTextChanged(otherWatchers, formattedEditableText);
    addTextChangedListener(editText, mainWatcher);
}
 
Example 15
Source File: Utils.java    From HgLauncher with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Handles common input shortcut from an EditText.
 *
 * @param activity The activity to reference for copying and pasting.
 * @param editText The EditText where the text is being copied/pasted.
 * @param keyCode  Keycode to handle.
 *
 * @return True if key is handled.
 */
public static boolean handleInputShortcut(AppCompatActivity activity, EditText editText, int keyCode) {
    // Get selected text for cut and copy.
    int start = editText.getSelectionStart();
    int end = editText.getSelectionEnd();
    final String text = editText.getText().toString().substring(start, end);

    switch (keyCode) {
        case KeyEvent.KEYCODE_A:
            editText.selectAll();
            return true;
        case KeyEvent.KEYCODE_X:
            editText.setText(editText.getText().toString().replace(text, ""));
            return true;
        case KeyEvent.KEYCODE_C:
            ActivityServiceUtils.copyToClipboard(activity, text);
            return true;
        case KeyEvent.KEYCODE_V:
            editText.setText(
                    editText.getText().replace(Math.min(start, end), Math.max(start, end),
                            ActivityServiceUtils.pasteFromClipboard(activity), 0,
                            ActivityServiceUtils.pasteFromClipboard(activity).length()));
            return true;
        default:
            // Do nothing.
            return false;
    }
}
 
Example 16
Source File: Editor.java    From RichEditor with Apache License 2.0 5 votes vote down vote up
/**
 * 编辑器处于选中状态
 */
private boolean checkEditorSelectStatus() {
    EditText etCur = richEditor.getCurEditText();
    if (etCur.getSelectionStart() != etCur.getSelectionEnd()) {
        return true;
    }
    return false;
}
 
Example 17
Source File: BaseConverterFragment.java    From text_converter with GNU General Public License v3.0 5 votes vote down vote up
private void insert(CharSequence text) {
    EditText editText = getCurrentEditText();
    if (editText != null) {
        int selectionStart = editText.getSelectionStart();
        int selectionEnd = editText.getSelectionEnd();
        selectionStart = Math.max(0, selectionStart);
        selectionEnd = Math.max(0, selectionEnd);
        editText.getText().replace(selectionStart, selectionEnd, "");
        editText.getText().insert(selectionStart, text);
    }

}
 
Example 18
Source File: EmojiconsFragment.java    From emojicon with Apache License 2.0 5 votes vote down vote up
public static void input(EditText editText, Emojicon emojicon) {
    if (editText == null || emojicon == null) {
        return;
    }

    int start = editText.getSelectionStart();
    int end = editText.getSelectionEnd();
    if (start < 0) {
        editText.append(emojicon.getEmoji());
    } else {
        editText.getText().replace(Math.min(start, end), Math.max(start, end), emojicon.getEmoji(), 0, emojicon.getEmoji().length());
    }
}
 
Example 19
Source File: AutoCompleteProvider.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
private int findChar(EditText editor, String s) {
    int selectionEnd = editor.getSelectionEnd();
    while (selectionEnd > -1 && editor.getText().charAt(selectionEnd) != s.charAt(0)) {
        selectionEnd--;
    }
    return selectionEnd;
}
 
Example 20
Source File: SketchFile.java    From APDE with GNU General Public License v2.0 3 votes vote down vote up
public FileChange getFileChange() {
	EditText code = fragment.getCodeEditText();
	
	if (code == null) {
		return null;
	}
	
	String codeText = code.getText().toString();
	
	if (!text.equals(codeText)) {
		HorizontalScrollView scrollerX = fragment.getCodeScrollerX();
		ScrollView scrollerY = fragment.getCodeScroller();
		
		FileChange change = new FileChange();
		
		getTextChange(change, text, codeText);
		
		change.beforeSelectionStart = selectionStart;
		change.beforeSelectionEnd = selectionEnd;
		
		change.afterSelectionStart = code.getSelectionStart();
		change.afterSelectionEnd = code.getSelectionEnd();
		
		change.beforeScrollX = scrollX;
		change.beforeScrollY = scrollY;
		
		change.afterScrollX = scrollerX.getScrollX();
		change.afterScrollY = scrollerY.getScrollY();
		
		return change;
	}
	
	return null;
}