简体   繁体   中英

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 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:

在此处输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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