简体   繁体   English

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

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

My app displays a long line of text like this:我的应用程序显示一长串文本,如下所示:

在此处输入图像描述

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:最后是这样的:

在此处输入图像描述

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?任何人有任何想法如何做到这一点?

I've tried to achieve what you need.我试图达到你的需要。

Text:文本:

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum 只是印刷和排版行业的虚拟文本。 Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.自 1500 年代以来,Lorem Ipsum 一直是行业的标准虚拟文本。 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.它在 1960 年代随着包含 Lorem Ipsum 段落的 Letraset 工作表的发布而流行,最近随着桌面出版软件如 Aldus PageMaker 的发布而流行,其中包括 Lorem Ipsum 的版本。

Code to add ">" in each line of TextView :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: output:

在此处输入图像描述

I ended up changing the field containing the original text to a TextView and creating an EditText for the reply.我最终将包含原始文本的字段更改为 TextView 并为回复创建了一个 EditText。 I didn't have to prefix the original with ">";我不必在原件前加上“>”; instead, I just italicized it to distinguish it from the reply.相反,我只是将它标为斜体以将其与回复区分开来。

暂无
暂无

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

相关问题 如何让我的Android应用生成随机数? - How do I make my Android app generate a random number? 如何使由setContentView()设置的textview(具有透明背景)中的“文本”显示在activity.xml中的布局上方? - How do I make a “text” from textview(with a transparent background) set by setContentView( ) be displayed above my layout in activity.xml? 如何让我的应用看起来像一个标准的Android应用程序? - How do I make my app look like a standard Android app? 在 Android 应用程序中,如何使用透明文本制作线性视图,以便父视图可见? - In an Android app, how do I make a linear view with transparent text so that the parent view is visible? 如何使PhoneGap Android APP适应所有设备? - How do I make my PhoneGap Android APP fit all devices? 如何让我的应用程序出现在app chooser中? - How do I make my app appear in app chooser? 如何从调用Android应用程序的快捷方式中读取标签文本? - How do I read the label text from the shortcut that invoked my Android App? 如何为字符串数组中的每个元素添加一个字符串前缀? - How do I prefix a String to each element in an array of Strings? 当我运行我的应用程序时,不会显示图像,但是会显示文本等 - When I run my app the images are not displayed however the text etc is Android Studios:如何编写代码,让我的应用程序在多次点击后执行特定操作? - Android Studios: How do I make a code that allows my app to do a certain action after a written number of clicks?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM