Android跳转TextView中的超链接并拦截点击事件

可跳转可拦截点击事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class DeepLinkActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_deeplink);

TextView textView = findViewById(R.id.textview);
textView.setText(getClickableHtml("<a href='scbeasysim://purchase/01219ef3-f8ab-82e5-ba03-a876d88d0263'>前往 SCB 支付</a>"));
textView.setMovementMethod(LinkMovementMethod.getInstance());
}

/**
* 格式化超链接文本内容并设置点击处理
*/
private 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) {
Log.i(LianConstants.TAG, "URL: " + urlSpan.getURL());
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlSpan.getURL()));
DeepLinkActivity.this.startActivity(intent);
}
};

// The original URLSpan needs to be removed to block the behavior of browser opening
clickableHtmlBuilder.removeSpan(urlSpan);
clickableHtmlBuilder.setSpan(clickableSpan, start, end, flags);
}
}

可跳转但不能拦截点击事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class DeepLinkActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_deeplink);

TextView textView = findViewById(R.id.textview);
textView.setText(getClickableHtml("<a href='scbeasysim://purchase/01219ef3-f8ab-42e5-ba03-a474d88d0263'>前往 SCB 支付</a>"));
textView.setMovementMethod(LinkMovementMethod.getInstance());
}

/**
* 格式化超链接文本内容并设置点击处理
*/
private 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) {
Log.i(LianConstants.TAG, "URL: " + urlSpan.getURL());

}
};

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

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2021 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :