2012-06-14 16 views
5

¿Cómo consigo el texto que ha sido truncada por Android en una elipsis?Cómo obtener el texto ellipsized en una TextView

Tengo un TextView:

<TextView 
    android:layout_width="120dp" 
    android:layout_height="wrap_content" 
    android:ellipsize="end" 
    android:singleLine="true" 
    android:text="Um longo texto aqui de exemplo" /> 

En un dispositivo de este TextView se muestra así:

"Um longo texto a..."

¿Cómo consigo el resto del texto?

Busco algo así como getRestOfTruncate() que volver "qui de exemplo". .

+0

que fija título y el texto de su pregunta. Desearía tener una respuesta para ti, pero no creo que haya una manera de hacer esto. ¿Cuál es el caso de uso aquí? –

+0

Gracias @AustynMahoney, probablemente debería ser que se haga algo para hacerlo, de lo contrario, voy a construir algo, el problema es que va a ser mucho más difícil, pero si no publicar aquí – ademar111190

+1

Use 'android: text =" @ string/full_text "' en el diseño xml, y 'getString (R.string.full_text)' en código java siempre que lo necesite. – yorkw

Respuesta

5
String text = (String) textView.getText().subSequence(textView.getLayout().getEllipsisStart(0), textView.getText().length()); 
+0

trabajo perfecto, pero tengo que insertar su código dentro de un postRunnable porque en primer lugar el TextView debe diseñarse, en finishi hago esto: nueva Handler() postDelayed (ejecutable con su código, 1 milissegundo);. – ademar111190

+2

Esto solo funciona si tengo 'android: singleLine =" true "'. Si lo configuro como 'falso' y configuré' android: maxLines' en un número que no sea ', entonces este método siempre devuelve el texto completo como si no hubiera sido elipsado en absoluto. –

0

Usando textView.getLayout() getEllipsisStart (0) sólo funciona si android: SingleLine = "true"

Aquí es una solución que funcione si android: maxLines se establece:

public static String getEllipsisText(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(end); 
    } 

    return null; 
} 

Recuerde: esto funciona después de la vista ya es visible.

Uso:

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