繁体   English   中英

Android edittext将每行上的文本放入自动换行的数组中?

[英]Android edittext get text on each line into an array with word wrap on?

我知道您可以通过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;
}

如果您知道编辑文本有多少个/多少个字符,则可以根据大小分割每个X个字符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM