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

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

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


当前回答

有一个名为append for ediitext的函数,它将字符串值追加到当前edittext值,并将光标放在值的末尾。 你可以将字符串值本身作为当前编辑文本值,并调用append();

myedittext.append("current_this_edittext_string"); 

其他回答

科特林:

将光标设置到起始位置:

val editText = findViewById(R.id.edittext_id) as EditText
editText.setSelection(0)

将光标设置到EditText的末尾:

val editText = findViewById(R.id.edittext_id) as EditText
editText.setSelection(editText.text.length)

下面的Code是将光标放在第二个字符之后:

val editText = findViewById(R.id.edittext_id) as EditText
editText.setSelection(2)

JAVA:

将光标设置到起始位置:

 EditText editText = (EditText)findViewById(R.id.edittext_id);
 editText.setSelection(0);

将光标设置到EditText的末尾:

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

下面的Code是将光标放在第二个字符之后:

EditText editText = (EditText)findViewById(R.id.edittext_id);
editText.setSelection(2);

试试这个:

<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

用于ViewModel, LiveData和Data绑定

我需要这个功能的EditText与多行支持在我的笔记应用程序。我想在文本结尾的光标,当用户导航到有笔记文本的片段。

djleop建议的解决方案很接近。但这样做的问题是,如果用户将光标放在文本中间进行编辑并开始输入,光标将再次跳转到文本的末尾。这是因为LiveData会发出新的值,光标会再次跳转到文本的末尾,导致用户无法编辑中间的文本。

为了解决这个问题,我使用MediatorLiveData,并使用标志仅为其分配一次String长度。这将导致LiveData只读取值一次,即当用户导航到片段时。在此之后,用户可以将光标放在任何他们想要编辑文本的地方。

ViewModel

private var accessedPosition: Boolean = false

val cursorPosition = MediatorLiveData<Event<Int>>().apply {
    addSource(yourObject) { value ->
        if(!accessedPosition) {
            setValue(Event(yourObject.note.length))
            accessedPosition = true
        }
    }
}

在这里,youobjecject是从数据库检索到的另一个LiveData,该数据库包含您在EditText中显示的String文本。

然后使用绑定适配器将此MediatorLiveData绑定到EditText。

XML

使用双向数据绑定来显示文本和接受文本输入。

<!-- android:text must be placed before cursorPosition otherwise we'll get IndexOutOfBounds exception-->
<EditText
    android:text="@={viewModel.noteText}"
    cursorPosition="@{viewModel.cursorPosition}" />

绑定适配器

@BindingAdapter("cursorPosition")
fun bindCursorPosition(editText: EditText, event: Event<Int>?) {
    event?.getContentIfNotHandled()?.let { editText.setSelection(it) }
}

事件类

这里的Event类类似于由Jose Alcérreca从谷歌编写的SingleLiveEvent。我在这里使用它来处理屏幕旋转。使用单个事件将确保当用户编辑文本在中间的某个地方并且屏幕旋转时,光标不会跳到文本的末尾。当屏幕旋转时,它将保持相同的位置。

下面是Event类:

open class Event<out T>(private val content: T) {

    var hasBeenHandled = false
        private set // Allow external read but not write

    /**
     * Returns the content and prevents its use again.
     */
    fun getContentIfNotHandled(): T? {
        return if (hasBeenHandled) {
            null
        } else {
            hasBeenHandled = true
            content
        }
    }

    /**
     * Returns the content, even if it's already been handled.
     */
    fun peekContent(): T = content
}

这是适合我的解决方案,提供了良好的用户体验。希望它对你的项目也有帮助。

完全工作的解决方案!!

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.setText("");
    if (!TextUtils.isEmpty(text)) {
        editText.append(text);
    }