我正在获取一个html字符串,我想从中提取href字符串,并且只有这些值可以点击才能在TextView中设置之前启动一些自定义活动。我试过这个
CharSequence sequence = Html.fromHtml(textPost.getDescription());
SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);
for(URLSpan span : urls) {
int start = strBuilder.getSpanStart(span);
int end = strBuilder.getSpanEnd(span);
int flags = strBuilder.getSpanFlags(span);
ClickableSpan clickable = new ClickableSpan() {
public void onClick(View view) {
Log.d("Home Activity", "Clicked yay");
}
};
strBuilder.setSpan(clickable, start, end, flags);
strBuilder.removeSpan(span);
}
textHolder.content.setText(strBuilder);
这是对文字的突出显示,但当我点击它们时,日志信息不会被打印
答案 0 :(得分:0)
从字符串
中提取超链接Pattern.compile("[a-z]+:\/\/[^ \n]*").matcher(
"download_recording true Package available http://ab.com click the link").find();
并使其可点击
TextView tv = (TextView) findViewById(R.id.textHolder);
tv.setText(Html.fromHtml(getString(R.string.sampleText)));
tv.setMovementMethod(LinkMovementMethod.getInstance());