下面列出了android.widget.EditText#getEditableText ( ) 实例代码,或者点击链接到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 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);//光标所在位置插入文字
}
}
}
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());
}
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);//光标所在位置插入文字
}
}
}
/**
* 修改加粗样式
*/
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 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);
}
/**
* 修改加粗斜体样式
*/
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);
}
/**
* 修改删除线样式
*/
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);
}
/**
* 修改下划线样式
*/
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);
}
/**
* 设置输入框的光标到末尾
*/
public static void setEditTextSelectionToEnd(EditText editText) {
Editable editable = editText.getEditableText();
Selection.setSelection((Spannable) editable, editable.toString().length());
}
/**
* 设置输入框的光标到末尾
*/
public static void setEditTextSelectionToEnd(EditText editText) {
Editable editable = editText.getEditableText();
Selection.setSelection((Spannable) editable,
editable.toString().length());
}
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;
}
}
}