私は、ScrollViewでラップされているTextViewに長い継承を持っています。現在表示されているテキストを見つける方法はありますか?テキストビューで現在の可視テキストを取得
テキストビューで行数、行の高さ、scrollviewからスクロールすることもできますが、現在表示されているテキストとのリンクを見つけることができます。助けてください!ありがとう。
私は、ScrollViewでラップされているTextViewに長い継承を持っています。現在表示されているテキストを見つける方法はありますか?テキストビューで現在の可視テキストを取得
テキストビューで行数、行の高さ、scrollviewからスクロールすることもできますが、現在表示されているテキストとのリンクを見つけることができます。助けてください!ありがとう。
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
これを行うには簡単です:
int start = textView.getLayout().getLineStart(0);
int end = textView.getLayout().getLineEnd(textView.getLineCount() - 1);
String displayed = textView.getText().toString().substring(start, end);
nullポインタ例外を受け取っても機能しません – Jayesh
はgetEllipsisStart()ここで
int end = textView.getLayout().getEllipsisStart(0);
これはsingleLine = "true" –
を使用してみてください。最初に表示された行の行番号を取得します。次に、2番目に表示された行の行番号を取得します。その後、テキストを取得し、単語の数を数えます。
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;
}
私自身もほぼ同じ問題を抱えています。私は、現在、recyclerviewに表示されているtextviewの最初の可視線が必要でした。 。textView.getLayout()を使用
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);
アンドロイド場合getEllipsisStart(0)のみ動作します:
単一行は=「true」を、あなたは現在、次のコードを使用することができrecyclerview中のTextViewの最初の行を表示取得しようとしている場合はここアンドロイドあれば動作しますソリューションです。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