简体   繁体   中英

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?

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. 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);

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