texto studio programacion plain mostrar letterspacing example español android textview

programacion - textview android studio español



Obtener el texto visible actual en textview (7)

Aquí. Obtenga el número de línea de la primera línea mostrada. Luego obtenga el número de línea de la segunda línea mostrada. Luego obtén el texto y cuenta el número de palabras.

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; }

Tengo un pasaje largo en un TextView que está enrollado alrededor de ScrollView. ¿Hay alguna manera de encontrar el texto visible actual?

Puedo encontrar el número de líneas, la altura de la línea en la vista de texto y también scrollx y scrolly desde la vista de desplazamiento, pero encontrar el enlace al texto actual mostrado. ¡Por favor ayuda! Gracias.


Es simple hacer esto:

int start = textView.getLayout().getLineStart(0); int end = textView.getLayout().getLineEnd(textView.getLineCount() - 1); String displayed = textView.getText().toString().substring(start, end);


Esto depende del uso de Ellipsize en TextView. Prueba esto:

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


Usar textView.getLayout (). GetEllipsisStart (0) solo funciona si android: singleLine = "true"

Aquí hay una solución que funcionará si se establece android: 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; }

Recuerde: esto funciona después de que la vista ya está cargada.

Uso:

textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { textView.getViewTreeObserver().removeOnGlobalLayoutListener(this); Log.i("test" ,"VisibleText="+getVisibleText(textView)); } });


Usted afirma que conoce scrollY , el número actual de píxeles desplazados. También conoce la altura de la ventana que está considerando en píxeles, así que llame a scrollViewHeight . Entonces

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

Es una aproximación, así que úsala como último recurso.


Yo también tengo el mismo problema. Necesitaba la primera línea visible de la vista de texto actualmente visible en recyclerview. Si está intentando obtener la primera línea de vista de texto que se muestra actualmente en recyclerview, puede usar el siguiente código:

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


intente usar getEllipsisStart ()

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