java - 如何在 EditText 中插入文本?

标签 java android android-edittext

如何在 EditText 中的光标位置插入一段文字? editText.setText 删除当前文本并设置新文本,这不是我想要的。 editText.setText(editText.getText() + "my text"); 在末尾插入文本,也不是这种情况。

最佳答案

这很容易做到:

int start =editText.getSelectionStart(); //Get cursor position with this function
String str = "my text";//String you want to insert
editText.getText().insert(start, str); //This will get the text and insert the String str at the current position.

希望对您有所帮助!

关于java - 如何在 EditText 中插入文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27723002/

相关文章:

android - 在 Activity/普通类中获取 Android 共享首选项值

javascript - 如何按每个条目右侧的语言对此 JSON 对象进行排序?

android - 在 kotlin 中创建通用的高阶函数

android - 如何更改 EditText 光标高度?

java - JAXB如何处理继承

java - JPA:如何具有相同实体类型的一对多关系

java - setPluginEnabled 未定义

java - 在 JTable 中使用 JButton 和 Action 有哪些缺点?

android - 如何将 editText 放在屏幕底部并使其高度可扩展

android - 如何在不同的膨胀线性布局 View 中设置具有相同 id 的 edittext 的值?