简体   繁体   English

在Android中获取多行edittext中特定行的内容

[英]Getting content of a particular line in a multiline edittext in android

I have a multiline edit text in which the number of lines can grow up to any number. 我有一个多行编辑文本,其中行数可以增加到任意数量。

Does android provide any functionality to get content of any particular line? android是否提供任何功能来获取任何特定行的内容?

Suppose total number of lines are three and i want to read only second line. 假设总行数为三,我只想读第二行。

A EditView in Android does wrap its text so it fits in the view. Android中的EditView确实会包装其文本,使其适合视图。 That means that you have to determine a lines start and end position to read it and then you can extract it from the full contentstring. 这意味着您必须确定一行的开始和结束位置才能读取它,然后才能从完整的内容字符串中提取它。 This post already explained this issue. 这篇文章已经解释了这个问题。

So your code would be: 因此您的代码将是:

 // change to your needs
 int linenumber = 1;

 int startPos = myTextView.getLayout().getLineStart(linenumber);
 int endPos = myTextView.getLayout().getLineEnd(linenumber);

 String theLine = myTextView.getText().toString().substring(startPos, endPos);

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

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