我正在更改keyListener上的EditText的值。
但是当我更改文本时,光标移动到EditText的开头。 我需要光标在文本的末尾。
如何将光标移动到EditText文本的末尾。
我正在更改keyListener上的EditText的值。
但是当我更改文本时,光标移动到EditText的开头。 我需要光标在文本的末尾。
如何将光标移动到EditText文本的末尾。
当前回答
试试这个:
<EditText
android:id="@+id/edt_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
android:cursorVisible="true"/>
在你的代码中:
EditText editText = findViewById(R.id.editText);
editText.setSelection(editText.getText().length()); // Set cursor end point
其他回答
这是
Editable etext = edittext.getText();
Selection.setSelection(etext,edittext.getText().toString().length());
etSSID.setSelection(etSSID.getText().length());
我认为这可以达到你想要的效果。
Editable etext = mSubjectTextEditor.getText();
Selection.setSelection(etext, etext.length());
完全工作的解决方案!!
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.accessibilityDelegate = object : View.AccessibilityDelegate() {
override fun sendAccessibilityEvent(host: View?, eventType: Int) {
super.sendAccessibilityEvent(host, eventType)
if (eventType == TYPE_VIEW_TEXT_SELECTION_CHANGED) {
editText.setSelection(editText.length())
}
}
}