有两个EditText,在加载页面时,在第一个EditText中设置了一个文本,所以现在光标将位于EditText的起始位置,我想在第二个EditText中设置光标的位置,其中不包含数据。如何做到这一点?


当前回答

使用下面的行

e2.setSelection(e2.length());

e2是编辑文本对象名称

其他回答

在kotlin中,你可以像这样创建一个扩展函数:

fun EditText.placeCursorAtLast() {
    val string = this.text.toString()
    this.setSelection(string.length)
}

然后简单地调用myEditText.placeCursorAtLast()

使用下面的行

e2.setSelection(e2.length());

e2是编辑文本对象名称

其中position为int型:

editText1.setSelection(position)

这将把光标移动到edittext的末尾

.toString myEditText.setSelection (myEditText.getText () () . length ())

如何设置编辑文本光标的位置在Android

下面的代码是将光标设置为开始在EditText:

 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());

下面的代码将光标设置在第2个字符位置之后:

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