我如何使我的Android应用程序前缀每个显示“&quot”的文本行?

发布于 2025-01-27 18:45:35 字数 871 浏览 1 评论 0原文

我的应用显示这样的一长串文本:

”在此处输入图像说明“

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

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();
}

并最终以此为:

“在此处输入图像说明”

请注意,只有一个“&gt;”整个文本的字符,而我试图使每个显示的行以“&gt;”开头。有人有什么想法吗?

My app displays a long line of text like this:

enter image description here

I want to add a ">" character to each line, so I tried the following code:

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();
}

and ended up with this:

enter image description here

Note that there's only one ">" character for the entire text, whereas I'm trying to make each displayed line start with ">". Anyone have any ideas how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我试图实现您的需求。

文本 :

lorem ipsum只是打印和排版的虚拟文本
行业。 Lorem Ipsum一直是该行业的标准虚拟文本
自1500年代以来。当未知的打印机带一个类型的厨房时
将其炒作以制作一本类型的标本书。它不仅幸存
五个世纪,但也飞向电子排版,
基本上保持不变。它在1960年代普及
释放包含lorem ipsum段落的LeTraset板,以及
最近使用桌面出版软件(例如Aldus Pagemaker)
包括lorem ipsum的版本。

代码添加“&gt;”在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);
        });

输出:

“在此处输入图像说明”

I've tried to achieve what you need.

Text :

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.

Code to add ">" in each line of 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);
        });

output :

enter image description here

作死小能手 2025-02-03 18:45:35

我最终将包含原始文本的字段更改为文本视图,并为答复创建诚意。我不必以“&gt;”为前缀。相反,我只是将其与答复区分开来。

I ended up changing the field containing the original text to a TextView and creating an EditText for the reply. I didn't have to prefix the original with ">"; instead, I just italicized it to distinguish it from the reply.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文