Android:在当前位置将文本插入 EditText
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3609174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: Insert text into EditText at current position
提问by Manuel
I want to insert a constant string into an EditText by the press of a button. The string should be inserted at the current position in the EditText.
If I use EditText.append
the text gets inserted at the end of the EditText.
我想通过按下按钮将一个常量字符串插入到 EditText 中。该字符串应插入到 EditText 中的当前位置。如果我使用EditText.append
文本被插入到 EditText 的末尾。
How can I do that? I couldn't find a suitable method.
我怎样才能做到这一点?我找不到合适的方法。
采纳答案by Cpt.Ohlund
Try using EditText.getSelectionStart()
to get the current position of the cursor. Then you can use String.subString to get the text before and after the cursor and insert your text in the middle.
尝试使用EditText.getSelectionStart()
获取光标的当前位置。然后您可以使用 String.subString 获取光标前后的文本并在中间插入文本。
回答by Manuel
Cpt.Ohlund gave me the right hint. I solved it, now, partly with using EditText.getSelectionStart()
, but I realized that you can also replace the selected text with the same expression and you don't need String.subString()
for that.
Cpt.Ohlund 给了我正确的提示。我现在解决了它,部分使用 using EditText.getSelectionStart()
,但我意识到您也可以用相同的表达式替换所选文本,而您不需要这样做String.subString()
。
int start = Math.max(myEditText.getSelectionStart(), 0);
int end = Math.max(myEditText.getSelectionEnd(), 0);
myEditText.getText().replace(Math.min(start, end), Math.max(start, end),
textToInsert, 0, textToInsert.length());
This works for both, inserting a text at the current position and replacing whatever text is selected by the user. The Math.max()
is necessary in the first and second line because, if there is no selection or cursor in the EditText, getSelectionStart()
and getSelectionEnd()
will both return -1. The Math.min()
and Math.max()
in the third line is necessary because the user could have selected the text backwards and thus start would have a higher value than end which is not allowed for Editable.replace()
.
这对两者都适用,在当前位置插入文本并替换用户选择的任何文本。的Math.max()
,因为,如果在没有的EditText选择或光标,在第一和第二行必要的getSelectionStart()
和getSelectionEnd()
都将返回-1。第三行中的Math.min()
andMath.max()
是必需的,因为用户可能已经向后选择了文本,因此 start 将具有比 end 更高的值,这是不允许的Editable.replace()
。
回答by Oleg Vaskevich
This seems simpler:
这似乎更简单:
yourEditText.getText().insert(yourEditText.getSelectionStart(), "fizzbuzz");
However, Manuel's answer might be better if you want to replace any selected text with the inserted text.
但是,如果您想用插入的文本替换任何选定的文本,Manuel 的答案可能会更好。
回答by wangzhengyi
I think this function will help for you :
我认为此功能会对您有所帮助:
public void insertConstantStr(String insertStr) {
String oriContent = editText.getText().toString();
int index = editText.getSelectionStart() >= 0 ? editText.getSelectionStart() : 0;
StringBuilder sBuilder = new StringBuilder(oriContent);
sBuilder.insert(index, insertStr);
editText.setText(sBuilder.toString());
editText.setSelection(index + insertStr.length());
}
回答by MSilva
Editable editable = new SpannableStringBuilder("Pass a string here");
Editable editable = new SpannableStringBuilder("在这里传递一个字符串");
yourEditText.text = editable;
yourEditText.text = 可编辑;