我正在更改keyListener上的EditText的值。

但是当我更改文本时,光标移动到EditText的开头。 我需要光标在文本的末尾。

如何将光标移动到EditText文本的末尾。


当前回答

您应该能够在EditText的方法setSelection()的帮助下实现这一点,请参阅这里

其他回答

试试这个:

更新:

科特林:

editText.setSelection(editText.length())//placing cursor at the end of the text

Java:

editText.setSelection(editText.getText().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())
        }
    }
}

这是

Editable etext = edittext.getText();
Selection.setSelection(etext,edittext.getText().toString().length());

如果你的EditText不清楚:

editText.setText("");
editText.append("New text");

or

editText.setText(null);
editText.append("New text");

editText。这里的setSelection很神奇。基本上选择让你把光标放在任何你想要的位置。

EditText editText = findViewById(R.id.editText);
editText.setSelection(editText.getText().length());

这将光标放置到EditText的末尾。基本上,editText.getText().length()会给出文本长度。然后使用setSelection with length。

editText.setSelection(0);

它用于将光标设置在起始位置(0)。