在textview中获取当前可见文本

时间:2011-12-26 15:17:31

标签: android textview

我在TextView中有一段很长的段落,它被ScrollView包裹着。有没有办法找到当前的可见文字?

我可以在textview中找到行数,行高,也可以在scrollview中找到scrollx和scrolly,但是找到当前显示文本的链接。请帮忙!感谢。

8 个答案:

答案 0 :(得分:15)

这样做很简单:

int start = textView.getLayout().getLineStart(0);
int end = textView.getLayout().getLineEnd(textView.getLineCount() - 1);

String displayed = textView.getText().toString().substring(start, end);

答案 1 :(得分:4)

下面。获取第一个显示行的行号。然后获取第二个显示行的行号。然后获取文本并计算单词数。

private int getNumberOfWordsDisplayed() {
        int start = textView.getLayout().getLineStart(getFirstLineIndex());
        int end = textView.getLayout().getLineEnd(getLastLineIndex());
        return textView.getText().toString().substring(start, end).split(" ").length;
    }

    /**
     * Gets the first line that is visible on the screen.
     *
     * @return
     */
    public int getFirstLineIndex() {
        int scrollY = scrollView.getScrollY();
        Layout layout = textView.getLayout();
        if (layout != null) {
            return layout.getLineForVertical(scrollY);
        }
        Log.d(TAG, "Layout is null: ");
        return -1;
    }

    /**
     * Gets the last visible line number on the screen.
     * @return last line that is visible on the screen.
     */
    public int getLastLineIndex() {
        int height = scrollView.getHeight();
        int scrollY = scrollView.getScrollY();
        Layout layout = textView.getLayout();
        if (layout != null) {
            return layout.getLineForVertical(scrollY + height);
        }
        return -1;
    }

答案 2 :(得分:1)

您声称知道scrollY,当前滚动的像素数。您还知道您正在考虑的窗口高度(以像素为单位),因此请调用scrollViewHeight。然后

int scrollY; // This is your current scroll position in pixels.
int scrollViewHeight; // This is the height of your scrolling window.
TextView textView; // This is the TextView we're considering.

String text = (String) textView.getText();
int charsPerLine = text.length() / textView.getLineCount();
int lineHeight = textView.getLineHeight();

int startLine = scrollY / lineHeight;
int endLine = startLine + scrollViewHeight/lineHeight + 1;

int startChar = charsPerLine * startLine;
int endChar = charsPerLine * (endLine+1) + 1;
String approxVisibleString = text.substring(startChar, endChar);

这是近似值,因此请将其作为最后的手段。

答案 3 :(得分:1)

我自己也有同样的问题。我需要在recyclelerview中当前可见的textview中的第一个可见行。如果您尝试在recyclerview中获取当前显示的第一行textview,您可以使用以下代码:

  TextView tv = (TextView) recyclerView.getChildAt(0); //gets current visible child view
  // this is for top visible 
  //view or the textview directly
   Rect r1 = new Rect();
   tv.getHitRect(r1);//gets visible rect of textview
   Layout l = tv.getLayout();

   int line = l.getLineForVertical(-1 * r1.top);//first visible line
   int start = l.getLineStart(line);//visible line start
   int end = l.getLineEnd(line);//visible line end

   String displayed = tv.getText().toString().substring(start, end);

答案 4 :(得分:0)

尝试使用getEllipsisStart()

int end = textView.getLayout().getEllipsisStart(0);

答案 5 :(得分:0)

使用textView.getLayout()。getEllipsisStart(0)仅在android:singleLine =“true”

时有效

如果设置了android:maxLines,这个解决方案将起作用:

<script src="popup.js"></script>

请记住:这已经在视图加载后有效。

<强>用法:

public static String getVisibleText(TextView textView) {
    // test that we have a textview and it has text
    if (textView==null || TextUtils.isEmpty(textView.getText())) return null;
    Layout l = textView.getLayout();
    if (l!=null) {
        // find the last visible position
        int end = l.getLineEnd(textView.getMaxLines()-1);
        // get only the text after that position
        return textView.getText().toString().substring(0,end);
    }

    return null;
}

答案 6 :(得分:0)

这取决于在TextView中使用Ellipsize。试试这个:

public String getVisibleText(TextView tv) {
    int lastLine = tv.getMaxLines() < 1 || tv.getMaxLines() > tv.getLineCount() ? tv.getLineCount() : tv.getMaxLines();
    if (tv.getEllipsize() != null && tv.getEllipsize().equals(TextUtils.TruncateAt.END)) {
        int ellCount = tv.getLayout().getEllipsisCount(lastLine - 1);
        if (ellCount > 0 && tv.length() > ellCount)
            return tv.getText().toString().substring(0, tv_title.getText().length() - ellCount);
        return tv.getText().toString();
    } else {
        int end = tv.getLayout().getLineEnd(lastLine - 1);
        return tv.getText().toString().substring(0, end);
    }
}

...

textView.post(new Runnable() {
    @Override
    public void run() {
        Log.d(TAG, getVisibleText(textView));
    }
});

答案 7 :(得分:0)

假设您具有滚动的行号,则可以使用以下命令获取显示的文本:

int start = tv.getLayout().getLineStart(scrolllinenumber);
int end=scrolllinenumber+tv.getLayout().getHeight();
String displayedtext = tv.getText().toString().substring(start, end);