我正在更改keyListener上的EditText的值。
但是当我更改文本时,光标移动到EditText的开头。 我需要光标在文本的末尾。
如何将光标移动到EditText文本的末尾。
我正在更改keyListener上的EditText的值。
但是当我更改文本时,光标移动到EditText的开头。 我需要光标在文本的末尾。
如何将光标移动到EditText文本的末尾。
当前回答
etSSID.setSelection(etSSID.getText().length());
其他回答
有一个名为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) }
}
}
如果你的EditText不清楚:
editText.setText("");
editText.append("New text");
or
editText.setText(null);
editText.append("New text");
如果您想将光标放置在EditText视图中文本的末尾
EditText rename;
String title = "title_goes_here";
int counts = (int) title.length();
rename.setSelection(counts);
rename.setText(title);
这是另一种可能的解决方案:
et.append("");
如果因为某种原因不管用,试试下面的方法:
et.setSelection(et.getText().length());