Cómo convertir SpannedString a Spannable

Hola, he definido un texto en un textview.

TextView tweet = (TextView) vi.findViewById(R.id.text); tweet.setText(Html.fromHtml(sb.toString())); 

Entonces necesito convertir el texto del TextView en Spannble . Así que hice esto como:

 Spannable s = (Spannable) tweet.getText(); 

Necesito convertir Spannable porque pasé el TextView en una función:

  private void stripUnderlines(TextView textView) { Spannable s = (Spannable) textView.getText(); URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class); for (URLSpan span : spans) { int start = s.getSpanStart(span); int end = s.getSpanEnd(span); s.removeSpan(span); span = new URLSpanNoUnderline(span.getURL()); s.setSpan(span, start, end, 0); } textView.setText(s); } private class URLSpanNoUnderline extends URLSpan { public URLSpanNoUnderline(String url) { super(url); } @Override public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); ds.setUnderlineText(false); } } 

Esto no muestra ningún error / advertencia. Pero lanzando un error de tiempo de ejecución:

 java.lang.ClassCastException: android.text.SpannedString cannot be cast to android.text.Spannable 

¿Cómo puedo convertir el SpannedStringt / texto de la vista de texto en Spannble? ¿O puedo hacer la misma tarea con SpannedString dentro de la función?

¿Cómo puedo convertir el SpannedStringt / texto de la vista de texto en Spannble?

new SpannableString(textView.getText()) debería funcionar.

¿O puedo hacer la misma tarea con SpannedString dentro de la función?

Lo sentimos, pero removeSpan() y setSpan() son métodos en la interfaz Spannable , y SpannedString no implementa Spannable .

Esto debe ser el trabajo correcto alrededor. Es tarde, pero alguien podría necesitarlo en el futuro

 private void stripUnderlines(TextView textView) { SpannableString s = new SpannableString(textView.getText()); URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class); for (URLSpan span : spans) { int start = s.getSpanStart(span); int end = s.getSpanEnd(span); s.removeSpan(span); span = new URLSpanNoUnderline(span.getURL()); s.setSpan(span, start, end, 0); } textView.setText(s); } private class URLSpanNoUnderline extends URLSpan { public URLSpanNoUnderline(String url) { super(url); } @Override public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); ds.setUnderlineText(false); } } 

Desafortunadamente ninguno de estos funcionó para mí, pero después de jugar con todas sus soluciones encontré algo que funcionó.

Se producirá un error al transmitir el textView.getText () a Spannable a menos que lo especifique como SPANNABLE

También tenga en cuenta la página de @ CommonsWare:

Tenga en cuenta que no desea llamar a setText () en TextView, pensando que estaría reemplazando el texto por la versión modificada. Está modificando el texto de TextView en su lugar en este método fixTextView () y, por tanto, setText () no es necesario. Peor aún, si está utilizando android: autoLink, setText () podría hacer que Android vuelva a través y agregue URLSpans de nuevo.

 accountAddressTextView.setText(accountAddress, TextView.BufferType.SPANNABLE); stripUnderlines(accountAddressTextView); private void stripUnderlines(TextView textView) { Spannable entrySpan = (Spannable)textView.getText(); URLSpan[] spans = entrySpan.getSpans(0, entrySpan.length(), URLSpan.class); for (URLSpan span: spans) { int start = entrySpan.getSpanStart(span); int end = entrySpan.getSpanEnd(span); entrySpan.removeSpan(span); span = new URLSpanNoUnderline(entrySpan.subSequence(start, end).toString()); entrySpan.setSpan(span, start, end, 0); } } 

Si especifica BufferType.SPANNABLE al configurar el texto de TextView , al obtener el texto puede convertirlo en Spannable

 myTextView.setText("hello", TextView.BufferType.SPANNABLE); ... ... ... Spannable str = (Spannable) myTextView.getText(); 
  • Cómo obtener el ancho del texto cuando el ajuste de varios spannable
  • mostrar emoticonos con Spannable en una vista de texto
  • Cómo aplicar la viñeta y la lista numerada en android Edit-text
  • SpannableString: ¿Es posible aplicar dos o más RelativeSizeSpans?
  • Espaciado de párrafos con SpannableStringBuilder en TextView
  • Cómo manejar evento onClick en imageSpan en editText?
  • Alinear texto alrededor del centro vertical de ImageSpan
  • ¿Cómo hacer clic o tocar en un texto TextView en diferentes palabras?
  • Dibuja un personaje con 2 colores en Android
  • Combinación de Spannable con String.format ()
  • Android Development: Cómo reemplazar parte de un EditText con un Spannable
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.