我正在更改keyListener上的EditText的值。
但是当我更改文本时,光标移动到EditText的开头。 我需要光标在文本的末尾。
如何将光标移动到EditText文本的末尾。
我正在更改keyListener上的EditText的值。
但是当我更改文本时,光标移动到EditText的开头。 我需要光标在文本的末尾。
如何将光标移动到EditText文本的末尾。
当前回答
如果你的EditText不清楚:
editText.setText("");
editText.append("New text");
or
editText.setText(null);
editText.append("New text");
其他回答
有一个名为append for ediitext的函数,它将字符串值追加到当前edittext值,并将光标放在值的末尾。 你可以将字符串值本身作为当前编辑文本值,并调用append();
myedittext.append("current_this_edittext_string");
完全工作的解决方案!!
kotlin,你需要用post
fun EditText.placeCursorToEnd() {
this.setOnFocusChangeListener { v, hasFocus ->
if (hasFocus) {
v.post { this.setSelection(text.length) }
}
}
this.setOnClickListener {
it.post { this.setSelection(text.length) }
}
}
在我的情况下,我创建了以下Kotlin ext.函数,可能对某人有用:
private fun EditText.focus(){
requestFocus()
setSelection(length())
}
然后按如下方式使用:
mEditText.focus()
试试这个:
更新:
科特林:
editText.setSelection(editText.length())//placing cursor at the end of the text
Java:
editText.setSelection(editText.getText().length());
这是
Editable etext = edittext.getText();
Selection.setSelection(etext,edittext.getText().toString().length());