TextView에 긴 구절이 있습니다.이 구절은 ScrollView에서 래핑됩니다. 현재 보이는 텍스트를 찾을 방법이 있습니까?Textview에서 현재 보이는 텍스트 가져 오기
textview에서 줄 수와 줄 높이를 찾을 수 있으며 scrollview에서 scrollx 및 scrolly를 찾을 수도 있지만 현재 표시된 텍스트로 연결되는 링크를 찾을 수 있습니다. 도와주세요! 감사.
TextView에 긴 구절이 있습니다.이 구절은 ScrollView에서 래핑됩니다. 현재 보이는 텍스트를 찾을 방법이 있습니까?Textview에서 현재 보이는 텍스트 가져 오기
textview에서 줄 수와 줄 높이를 찾을 수 있으며 scrollview에서 scrollx 및 scrolly를 찾을 수도 있지만 현재 표시된 텍스트로 연결되는 링크를 찾을 수 있습니다. 도와주세요! 감사.
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);
최하단이므로 사용하십시오.
제안 해 주셔서 감사합니다. 방금 시도 했으므로 근사값은 짧은 통행에 좋습니다. 내가 표시 할 긴 텍스트의 경우 오류가 너무 많습니다. 텍스트를 측정하고 계산을 너무 많이하지 않으면 TextPaint를 가져올 생각입니다. – John
시도 사용 getEllipsisStart(). 첫 번째로 표시된 행의 행 번호를 가져옵니다. 그런 다음 표시된 두 번째 줄의 줄 번호를 가져옵니다. 그런 다음 텍스트를 가져 와서 단어의 수를 계산하십시오.
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;
}
이것은 singleLine = "true"인 경우에만 작동합니다. –
여기
int start = textView.getLayout().getLineStart(0);
int end = textView.getLayout().getLineEnd(textView.getLineCount() - 1);
String displayed = textView.getText().toString().substring(start, end);
또한 나 자신과 거의 같은 문제가 있습니다. 나는 현재 recyclerview에서 볼 수있는 textview의 첫 번째 가시 선을 필요로했다. 당신이 얻을하려는 경우 현재 다음과 같은 코드를 사용할 수 있습니다 recyclerview에 텍스트 뷰의 첫 번째 줄을 표시 :.
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);
textView.getLayout을 사용하여()를 getEllipsisStart (0)에만 안드로이드 경우 작동 : 만일 Singleline는 = "true"를
여기에 안드로이드 경우 작동하는 솔루션입니다 : maxLines가 설정됩니다
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;
}
는 기억 뷰가 이미로드 된 후이 작동합니다.
사용법 :
textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
Log.i("test" ,"VisibleText="+getVisibleText(textView));
}
});
는 근사값은 괜찮습니까? – Snowball
안녕하세요. 나는 같은 문제에 대해 연구 중이다. 이 문제에 대한 해결책이 있습니까? TextView에서 텍스트의 보이는 부분을 찾아야합니다. 너 나 좀 도와 줄 수있어? –
내가 아는 한, TextView로이 작업을 수행 할 방법이 없다고 생각합니다. – kosa