首页 > 解决方案 > 如何在文本中间聚焦

问题描述

我有一个EditText多行由 Google 地址 API 生成的 Place + newline + Address 组成。

显示屏幕时,我想让文本光标位于地名末尾(第一行末尾)而不是EditText.

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

标签: javaandroid

解决方案


你说“光标”,但你有一个TextView,而不是一个EditText。如果您想要一个EditText以便用户可以与之交互,您可以使用setSelection方法并传递您希望光标旁边的字符的索引

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

推荐阅读