Android跳转TextView中的超链接并拦截点击事件
可跳转可拦截点击事件
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);
}
}
可跳转但不能拦截点击事件
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);
}
}
版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/30/android-redirects-to-hyperlinks-in-textview-and-intercepts-click-events/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。
THE END
1
二维码
打赏
海报
Android跳转TextView中的超链接并拦截点击事件
可跳转可拦截点击事件
public class DeepLinkActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInst……
文章目录
关闭
共有 0 条评论