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

后端 未结 2 773
梦谈多话
梦谈多话 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 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);
    }
    

提交回复
热议问题