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:
- TextView con diferentes textSize
- Android: "nowrap" en Spannable en TextView
- Cambiar el color del texto cuando el usuario marca las etiquetas hash
- MonoDroid GetSpans último parámetro
- En un EditText, ¿hay una manera de desactivar la autocorrección en un span?
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?
- Android SpannableString establecer fondo detrás de parte del texto
- Spannable en android para textView
- Cómo recuperar BackgroundColorSpan para el texto seleccionado en android
- StyleSpan no se aplica a SpannableString en TextView
- El botón setText con Spannable no funciona para Android 5.0 Lollipop
- SpannableString no se muestra correctamente en modo horizontal
- Cómo dibujar una cadena Spanned con Canvas.drawText en Android
- Handle hace clic en los vínculos de TextView mientras utiliza Linkify para encontrar y establecer los vínculos dentro del texto
¿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();
- Android: startActivityForResult () con funcionalidad de botón BACK
- Android: Los archivos que escribo en la tarjeta sd no se muestran en el explorador de Windows, para Acer Iconiatab