android.widget.EditText#getSelectionEnd ( )源码实例Demo

下面列出了android.widget.EditText#getSelectionEnd ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: MyBookshelf   文件: SourceEditActivity.java
@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);//光标所在位置插入文字
        }
    }
}
 
源代码2 项目: text_converter   文件: BaseConverterFragment.java
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);
            }
        }
    }
}
 
/**
 * 每次点击表情会调用,已经处理长按选择多个表情然后再输入表情的情况
 * @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());
    }
}
 
源代码4 项目: mvvm-template   文件: MarkDownProvider.java
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());
    }
}
 
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);
}
 
源代码6 项目: quill   文件: EditTextSelectionState.java
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;
    }
}
 
private int findChar(EditText editor, String s) {
    int selectionEnd = editor.getSelectionEnd();
    while (selectionEnd > -1 && editor.getText().charAt(selectionEnd) != s.charAt(0)) {
        selectionEnd--;
    }
    return selectionEnd;
}
 
源代码8 项目: emojicon   文件: EmojiconsFragment.java
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());
    }
}
 
源代码9 项目: text_converter   文件: BaseConverterFragment.java
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);
    }

}
 
源代码10 项目: RichEditor   文件: Editor.java
/**
 * 编辑器处于选中状态
 */
private boolean checkEditorSelectStatus() {
    EditText etCur = richEditor.getCurEditText();
    if (etCur.getSelectionStart() != etCur.getSelectionEnd()) {
        return true;
    }
    return false;
}
 
源代码11 项目: HgLauncher   文件: Utils.java
/**
 * 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;
    }
}
 
源代码12 项目: WhatsappFormatter   文件: WhatsappViewCompat.java
/**
 * 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);
}
 
源代码13 项目: mvvm-template   文件: MarkDownProvider.java
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());

}
 
源代码14 项目: mvvm-template   文件: MarkDownProvider.java
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);

}
 
源代码15 项目: mvvm-template   文件: MarkDownProvider.java
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);

}
 
源代码16 项目: mvvm-template   文件: MarkDownProvider.java
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);

}
 
源代码17 项目: mvvm-template   文件: MarkDownProvider.java
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);

}
 
源代码18 项目: android-discourse   文件: EditorFragment.java
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);
    }
}
 
源代码19 项目: YCCustomText   文件: SpanTextHelper.java
/**
 * 修改加粗样式
 */
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);
}
 
源代码20 项目: APDE   文件: SketchFile.java
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;
}