1 利用SpannableStringBuilder获取存在的Span
/**
* Return an array of the spans of the specified type that overlap
* the specified range of the buffer. The kind may be Object.class to get
* a list of all the spans regardless of type.
*/
@SuppressWarnings(“unchecked”)
public T[] getSpans(int queryStart, int queryEnd, @Nullable Class kind) {
return getSpans(queryStart, queryEnd, kind, true);
}
getSpans可以获取SpannableStringBuilder中被设置的Span数组,参数说明:
queryStart:开始位置
queryEnd:结束位置
kind:span对应的Class
例子:
setContentView(R.layout.activity_urlspan_click);
mTextView = findViewById(R.id.textview);
SpannableStringBuilder spannableStringBuilder1 = new SpannableStringBuilder("测试富文本样式设");
ForegroundColorSpan fspan1 = new ForegroundColorSpan(Color.RED);
spannableStringBuilder1.setSpan(fspan1,4,8, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
SpannableString textSpanned1 = new SpannableString("置字体颜色设置");
ForegroundColorSpan fspan2 = new ForegroundColorSpan(Color.BLUE);
textSpanned1.setSpan(fspan2,0,textSpanned1.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
spannableStringBuilder1.append(textSpanned1);
mTextView.setText(spannableStringBuilder1);
ForegroundColorSpan[] spans = spannableStringBuilder1.getSpans(0, spannableStringBuilder1.length(), ForegroundColorSpan.class);
System.out.println("========spans.length========"+spans.length);
System.out.println("========SpanStart========"+spannableStringBuilder1.getSpanStart(spans[0]));
System.out.println("========SpanEnd========"+spannableStringBuilder1.getSpanEnd(spans[0]));
System.out.println("========SpanFlags========"+spannableStringBuilder1.getSpanFlags(spans[0]));
System.out.println("========SpanStart========"+textSpanned1.getSpanStart(spans[1]));
System.out.println("========SpanEnd========"+textSpanned1.getSpanEnd(spans[1]));
System.out.println("========SpanFlags========"+textSpanned1.getSpanFlags(spans[1]));
result:
========spans.length========2
========SpanStart========4
========SpanEnd=======15
========SpanFlags=======18
========SpanStart=======0
========SpanEnd=======7
========SpanFlags=======18
获取到两个设置的ForegroundColorSpan,然后可以利用获取到的ForegroundColorSpan得到调用setSpan时的信息。
2 利用URLSpan和ClickableSpan给html代码片段超链接设置点击
给html中的超链接设置点击事件:
public class URLSpanClickActivity extends AppCompatActivity {
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_urlspan_click);
mTextView = findViewById(R.id.textview);
/*SpannableStringBuilder spannableStringBuilder1 = new SpannableStringBuilder("测试富文本样式设");
ForegroundColorSpan fspan1 = new ForegroundColorSpan(Color.RED);
spannableStringBuilder1.setSpan(fspan1,4,8, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
SpannableString textSpanned1 = new SpannableString("置字体颜色设置");
ForegroundColorSpan fspan2 = new ForegroundColorSpan(Color.BLUE);
textSpanned1.setSpan(fspan2,0,textSpanned1.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
spannableStringBuilder1.append(textSpanned1);
mTextView.setText(spannableStringBuilder1);
ForegroundColorSpan[] spans = spannableStringBuilder1.getSpans(0, spannableStringBuilder1.length(), ForegroundColorSpan.class);
System.out.println("========spans.length========"+spans.length);
System.out.println("========SpanStart========"+spannableStringBuilder1.getSpanStart(spans[0]));
System.out.println("========SpanEnd========"+spannableStringBuilder1.getSpanEnd(spans[0]));
System.out.println("========SpanFlags========"+spannableStringBuilder1.getSpanFlags(spans[0]));
System.out.println("========SpanStart========"+textSpanned1.getSpanStart(spans[1]));
System.out.println("========SpanEnd========"+textSpanned1.getSpanEnd(spans[1]));
System.out.println("========SpanFlags========"+textSpanned1.getSpanFlags(spans[1]));*/
mTextView.setMovementMethod(LinkMovementMethod.getInstance());
String originalStr = "url 跳转的测试 <br/>" +
"<a href=\"https://www.baidu.com\" >百度</a> <br/>\n" +
"<a href=\"http://www.w3school.com.cn\">W3School</a>";
CharSequence stringBuilder = getClickableHtml(originalStr);
mTextView.setText(stringBuilder);
}
private CharSequence getClickableHtml(String html) {
Spanned spannedHtml = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
spannedHtml = Html.fromHtml(html, Html.FROM_HTML_MODE_COMPACT);
} else {
spannedHtml = Html.fromHtml(html);
}
SpannableStringBuilder clickableBuilder = new SpannableStringBuilder(spannedHtml);
URLSpan[] urls = clickableBuilder.getSpans(0, spannedHtml.length(), URLSpan.class);
if (urls.length == 0){
return html.replace("\\n", "\n").replace("\\r", "\r");
}
//获取所有的URL
for (final URLSpan span : urls) {
setLinkClickable(clickableBuilder, span);
}
return clickableBuilder;
}
//必须设置setMovementMethod(LinkMovementMethod.getInstance());ClickableSpan才会生效
private void setLinkClickable(final SpannableStringBuilder clickableHtmlBuilder, final URLSpan urlSpan) {
int start = clickableHtmlBuilder.getSpanStart(urlSpan);
int end = clickableHtmlBuilder.getSpanEnd(urlSpan);
int flags = clickableHtmlBuilder.getSpanFlags(urlSpan);
ClickableSpan clickableSpan = new ClickableSpan() {
public void onClick(View view) {
//可以获取html代码中的url地址,然后根据需要跳转特定页面
String url = urlSpan.getURL();
Toast.makeText(URLSpanClickActivity.this,"跳转url"+url,Toast.LENGTH_SHORT).show();
}
public void updateDrawState(TextPaint ds) {
//设置字体颜色
ds.setColor(Color.RED);
//去掉下划线
ds.setUnderlineText(false);
}
};
clickableHtmlBuilder.setSpan(clickableSpan, start, end, flags);
}
}