gpt4 book ai didi

java - Android edittext 将每行上的文本获取到一个数组中并自动换行?

转载 作者:行者123 更新时间:2023-12-01 18:31:22 26 4
gpt4 key购买 nike

我知道你可以通过 text.split("\n") 分割字符串,但这不适用于自动换行。还有别的办法吗?

最佳答案

这可以使用 Layout API 来完成。

附评论:

public static List<CharSequence> getLines(TextView view) {
final List<CharSequence> lines = new ArrayList<>();
final Layout layout = view.getLayout();

if (layout != null) {
// Get the number of lines currently in the layout
final int lineCount = layout.getLineCount();

// Get the text from the layout.
final CharSequence text = layout.getText();

// Initialize a start index of 0, and iterate for all lines
for (int i = 0, startIndex = 0; i < lineCount; i++) {
// Get the end index of the current line (use getLineVisibleEnd()
// instead if you don't want to include whitespace)
final int endIndex = layout.getLineEnd(i);

// Add the subSequence between the last start index
// and the end index for the current line.
lines.add(text.subSequence(startIndex, endIndex));

// Update the start index, since the indices are relative
// to the full text.
startIndex = endIndex;
}
}
return lines;
}

没有评论:

public static List<CharSequence> getLines(TextView view) {
final List<CharSequence> lines = new ArrayList<>();
final Layout layout = view.getLayout();

if (layout != null) {
final int lineCount = layout.getLineCount();
final CharSequence text = layout.getText();

for (int i = 0, startIndex = 0; i < lineCount; i++) {
final int endIndex = layout.getLineEnd(i);
lines.add(text.subSequence(startIndex, endIndex));
startIndex = endIndex;
}
}
return lines;
}

关于java - Android edittext 将每行上的文本获取到一个数组中并自动换行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24025290/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com