How do I get the text at a specific line in a TextView

后端 未结 2 771
梦谈多话
梦谈多话 2020-12-05 04:20

I have a TextView that is X lines long. How do I get the text that is at, say, line 3?

Example: I have this TextView

相关标签:
2条回答
  • 2020-12-05 04:49

    Here is some code to supplement the accepted answer:

    List<CharSequence> lines = new ArrayList<>();
    int count = textView.getLineCount();
    for (int line = 0; line < count; line++) {
        int start = textView.getLayout().getLineStart(line);
        int end = textView.getLayout().getLineEnd(line);
        CharSequence substring = textView.getText().subSequence(start, end);
        lines.add(substring);
    }
    
    0 讨论(0)
  • 2020-12-05 05:14

    You can use textView.getLayout().getLineStart(int line) and getLineEnd to find the character offsets in the text.

    Then you can just use textView.getText().substring(start, end) -- or subsequence if you are using Spannables for formatting/etc.

    0 讨论(0)
提交回复
热议问题