我如何获得在特定行的文本在一个TextView [英] How do I get the text at a specific line in a TextView

查看:128
本文介绍了我如何获得在特定行的文本在一个TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的TextView 的为X线长。如何获取的文字是,比如说,3号线?

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

例如:我有这个的TextView

这是1号线

这是2号线

这是第3行

我希望能够获得字符串在这些线路由itsself,例如获得字符串<中的任何一个/ code>第3行会返回这是第3行。搜索换行符是不是我需要的解决方案,因为它没有考虑到文字换行。

I want to be able to get the String at any one of these lines by itsself, such as getting the String at line 3 would return "And this is line 3". Searching for line breaks is not the solution I need, as it doesn't take into account text wrapping.

推荐答案

您可以使用textView.getLayout()。getLineStart(INT线)和getLineEnd找到字符偏移量中的文本。

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

然后,你可以使用textView.getText()子(开始,结束) - 或子,如果你使用的是Spannables进行格式化的/ etc

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

这篇关于我如何获得在特定行的文本在一个TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆