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

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

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


当前回答

用于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
}

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

其他回答

我认为这可以达到你想要的效果。

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

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

myedittext.append("current_this_edittext_string"); 
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())
        }
    }
}

如果您想将光标放置在EditText视图中文本的末尾

 EditText rename;
 String title = "title_goes_here";
 int counts = (int) title.length();
 rename.setSelection(counts);
 rename.setText(title);
public class CustomEditText extends EditText {
    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomEditText(Context context) {
        super(context);
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }


    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        this.setSelection(this.getText().length());
    }

    @Override
    protected void onSelectionChanged(int selStart, int selEnd) {

    }
}

在XML文件中使用此CustomEditText,这将起作用。我已经测试过了,它对我很有效。