如何专注于文本中间



我有一个带有多行的 EditText,由plot newline google地址API生成的地址。

显示屏幕时,我想让文本光标处的位置名称的末端(第一行的末端(不是EditText中的第一个字符。

String text = placeName + "n" + addressName;
TextView tv = findViewById(R.id.placeAddress);
tv.setText(text);

对上述代码进行小修改

String text = placeName + "n" + addressName;
EditText et = findViewById(R.id.placeAddress);
et.setText(text).setSelection(placeName.length());

您说"光标",但是您有一个TextView,而不是EditText。如果您想要EditText,以便用户可以与之进行交互,则可以使用SetSelection方法并传递您希望光标的索引

String text = placeName + "n" + addressName;
EditText et = findViewById(R.id.placeAddress);
et.setText(text);
et.setSelection(placeName.length());

最新更新