关闭

获取一段文字中的某一串并标记成URL,SpannableStringBuilder

标签: URLSpannableStringBuild
294人阅读 评论(0) 收藏 举报
public CharSequence getClickableHtml(String html) {
		Spanned spannedHtml = Html.fromHtml(html);
		SpannableStringBuilder clickableHtmlBuilder = new SpannableStringBuilder(spannedHtml);
		URLSpan[] urls = clickableHtmlBuilder.getSpans(0, spannedHtml.length(), URLSpan.class);
		for (final URLSpan span : urls) {
			setLinkClickable(clickableHtmlBuilder, span);
		}
		return clickableHtmlBuilder;
	}

	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) {
				// Do something with URL here.
				if (!TextUtils.isEmpty(urlSpan.getURL())) {
					int result = urlSpan.getURL().indexOf("tel");
					int result2 = urlSpan.getURL().indexOf("http");
					if (result >= 0) {
						Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(urlSpan.getURL()));
						MessageDetailsActivity.this.startActivity(intent);// 内部类
					} else if (result2 >= 0) {
						Intent intent = new Intent(MessageDetailsActivity.this, ShipWayArrivedExcDetailActivity.class);
						intent.putExtra("title", "订单详情");
						intent.putExtra("orderId", messageInfo.getCustomerOrderNo());
						intent.putExtra("url", urlSpan.getURL());
						startActivity(intent);
					}
				}

			}

		};
		clickableHtmlBuilder.setSpan(clickableSpan, start, end, flags);
	}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:708次
    • 积分:47
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档