18

Hello I have set some text in a textview.

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

Then I need to convert the text of the TextView into Spannble. So I did this like:

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

I need to convert it Spannable because I passed the TextView into a function:

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

This shows no error/warning. But throwing a Runtime Error:

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

How can I convert the SpannedStringt/text of the textview into Spannble? Or can I do the same task with SpannedString inside the function?

4

4 回答 4

23

如何将 textview 的 SpannedStringt/text 转换为 Spannble?

new SpannableString(textView.getText())应该管用。

或者我可以在函数内使用 SpannedString 执行相同的任务吗?

抱歉,removeSpan()andsetSpan()Spannable接口上的方法,并SpannedString没有实现Spannable.

于 2013-07-26T13:20:38.490 回答
2

这应该是正确的解决方法。它迟到了,但将来有人可能需要它

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);
    }
}
于 2016-01-14T20:34:12.607 回答
1

不幸的是,这些都不适合我,但是在摆弄了你所有的解决方案之后,我发现了一些有用的东西。

除非您将其指定为 SPANNABLE,否则它将错误地将 textView.getText() 转换为 Spannable

另请注意@CommonsWare 的页面:

请注意,您不想在 TextView 上调用 setText(),以为您将用修改后的版本替换文本。您正在此 fixTextView() 方法中修改 TextView 的文本,因此不需要 setText()。更糟糕的是,如果您使用的是 android:autoLink,setText() 会导致 Android 返回并再次添加 URLSpans。

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);
    }
}
于 2016-02-26T18:56:26.833 回答
0

如果您BufferType.SPANNABLE在设置TextView文本时指定,则在获取文本时可以将其转换为Spannable

myTextView.setText("hello", TextView.BufferType.SPANNABLE);
...
...
...
Spannable str = (Spannable) myTextView.getText();
于 2015-06-23T17:35:10.067 回答