如何让我的 Android 应用程序在每个显示的文本行前加上“>”?

How do I make my Android app prefix each displayed line of text with ">"?

我的应用程序显示一长串文本,如下所示:

我想在每一行添加一个“>”字符,所以我尝试了以下代码:

private String addPrefixChars(String text) {
    StringBuilder builder = new StringBuilder();
    StringTokenizer st = new StringTokenizer(text, NL);
    builder.append(NL);
    for (int i = 0, count = st.countTokens(); i < count; i++) {
        builder.append(">").append(st.nextToken()).append(NL);
    }
    return builder.toString();
}

最后是这样的:

请注意,整个文本只有一个“>”字符,而我试图让每个显示的行 以“>”开头。任何人有任何想法如何做到这一点?

我已经尽力满足您的需求。

正文:

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s. When an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

TextView的每一行中添加“>”的代码:

  TextView content = findViewById(R.id.content);
  content.post(() -> {
            ArrayList<String> lines = new ArrayList<>();
            String textContent = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s. When an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
            Layout layout = content.getLayout();
            int lineCount = content.getLineCount();
            for (int i = 0; i < lineCount; i++) {
                int lineStart = layout.getLineStart(i);
                int lineEnd = layout.getLineEnd(i);
                if (lineStart <= textContent.length() && lineEnd <= textContent.length()) {
                    String lineString = textContent.substring(lineStart, lineEnd);
                    lines.add("> " + lineString + "\n");
                }
            }
            content.setText("");
            for (String line : lines)
                content.append(line);
        });

输出:

我最终将包含原始文本的字段更改为 TextView 并为回复创建了 EditText。我不必在原件前加上“>”;相反,我只是将它标为斜体以区别于回复。