我有一个X行长的TextView
。我如何得到文本,例如,第3行?
例子:我有这个TextView
,这是第1行
这是第2行
这是第3行
我希望能够在这些行中的任何一行获得String
,例如在第3行获取String
将返回“这是第3行”。搜索换行不是我需要的解决方案,因为它没有考虑到文本包装。
发布于 2011-06-08 02:20:18
您可以使用textView.getLayout().getLineStart(int line)
和getLineEnd
查找文本中的字符偏移量。
然后,您可以只使用textView.getText().substring(start, end)
--或者如果您使用Spannable进行格式化/等等,则使用子序列。
发布于 2018-09-21 09:47:02
下面是一些代码来补充已接受的答案:
List<CharSequence> lines = new ArrayList<>();
int count = textView.getLineCount();
for (int line = 0; line < count; line++) {
int start = textView.getLayout().getLineStart(line);
int end = textView.getLayout().getLineEnd(line);
CharSequence substring = textView.getText().subSequence(start, end);
lines.add(substring);
}
https://stackoverflow.com/questions/6272614
复制相似问题