下面列出了android.widget.EditText#getSelectionEnd ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);//光标所在位置插入文字
}
}
}
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());
}
}
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);
}
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;
}
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());
}
}
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);
}
}
/**
* 编辑器处于选中状态
*/
private boolean checkEditorSelectStatus() {
EditText etCur = richEditor.getCurEditText();
if (etCur.getSelectionStart() != etCur.getSelectionEnd()) {
return true;
}
return false;
}
/**
* 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;
}
}
/**
* 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);
}
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());
}
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);
}
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);
}
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);
}
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);
}
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);
}
}
/**
* 修改加粗样式
*/
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);
}
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;
}