{"id":1878,"date":"2023-03-30T22:36:22","date_gmt":"2023-03-30T14:36:22","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1878"},"modified":"2023-04-22T09:02:41","modified_gmt":"2023-04-22T01:02:41","slug":"android-redirects-to-hyperlinks-in-textview-and-intercepts-click-events","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/30\/android-redirects-to-hyperlinks-in-textview-and-intercepts-click-events\/","title":{"rendered":"Android\u8df3\u8f6cTextView\u4e2d\u7684\u8d85\u94fe\u63a5\u5e76\u62e6\u622a\u70b9\u51fb\u4e8b\u4ef6"},"content":{"rendered":"<h3>\u53ef\u8df3\u8f6c\u53ef\u62e6\u622a\u70b9\u51fb\u4e8b\u4ef6<\/h3>\n<pre><code class=\"language-java\">public class DeepLinkActivity extends AppCompatActivity {\n\n    @Override\n    protected void onCreate(@Nullable Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_deeplink);\n\n        TextView textView = findViewById(R.id.textview);\n        textView.setText(getClickableHtml(&quot;&lt;a href=&#039;scbeasysim:\/\/purchase\/01219ef3-f8ab-82e5-ba03-a876d88d0263&#039;&gt;\u524d\u5f80 SCB \u652f\u4ed8&lt;\/a&gt;&quot;));\n        textView.setMovementMethod(LinkMovementMethod.getInstance());\n    }\n\n    \/**\n     * \u683c\u5f0f\u5316\u8d85\u94fe\u63a5\u6587\u672c\u5185\u5bb9\u5e76\u8bbe\u7f6e\u70b9\u51fb\u5904\u7406\n     *\/\n    private CharSequence getClickableHtml(String html) {\n        Spanned spannedHtml = Html.fromHtml(html);\n        SpannableStringBuilder clickableHtmlBuilder = new SpannableStringBuilder(spannedHtml);\n        URLSpan[] urls = clickableHtmlBuilder.getSpans(0, spannedHtml.length(), URLSpan.class);\n        for (final URLSpan span : urls) {\n            setLinkClickable(clickableHtmlBuilder, span);\n        }\n        return clickableHtmlBuilder;\n    }\n\n    \/**\n     * \u8bbe\u7f6e\u70b9\u51fb\u8d85\u94fe\u63a5\u5bf9\u5e94\u7684\u5904\u7406\u5185\u5bb9\n     *\/\n    private void setLinkClickable(final SpannableStringBuilder clickableHtmlBuilder, final URLSpan urlSpan) {\n        int start = clickableHtmlBuilder.getSpanStart(urlSpan);\n        int end = clickableHtmlBuilder.getSpanEnd(urlSpan);\n        int flags = clickableHtmlBuilder.getSpanFlags(urlSpan);\n\n        ClickableSpan clickableSpan = new ClickableSpan() {\n            public void onClick(View view) {\n                Log.i(LianConstants.TAG, &quot;URL: &quot; + urlSpan.getURL());\n                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlSpan.getURL()));\n                DeepLinkActivity.this.startActivity(intent);\n            }\n        };\n\n        \/\/ The original URLSpan needs to be removed to block the behavior of browser opening\n        clickableHtmlBuilder.removeSpan(urlSpan);\n        clickableHtmlBuilder.setSpan(clickableSpan, start, end, flags);\n    }\n}<\/code><\/pre>\n<p><!-- more --><\/p>\n<h3>\u53ef\u8df3\u8f6c\u4f46\u4e0d\u80fd\u62e6\u622a\u70b9\u51fb\u4e8b\u4ef6<\/h3>\n<pre><code class=\"language-java\">public class DeepLinkActivity extends AppCompatActivity {\n\n    @Override\n    protected void onCreate(@Nullable Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_deeplink);\n\n        TextView textView = findViewById(R.id.textview);\n        textView.setText(getClickableHtml(&quot;&lt;a href=&#039;scbeasysim:\/\/purchase\/01219ef3-f8ab-42e5-ba03-a474d88d0263&#039;&gt;\u524d\u5f80 SCB \u652f\u4ed8&lt;\/a&gt;&quot;));\n        textView.setMovementMethod(LinkMovementMethod.getInstance());\n    }\n\n    \/**\n     * \u683c\u5f0f\u5316\u8d85\u94fe\u63a5\u6587\u672c\u5185\u5bb9\u5e76\u8bbe\u7f6e\u70b9\u51fb\u5904\u7406\n     *\/\n    private CharSequence getClickableHtml(String html) {\n        Spanned spannedHtml = Html.fromHtml(html);\n        SpannableStringBuilder clickableHtmlBuilder = new SpannableStringBuilder(spannedHtml);\n        URLSpan[] urls = clickableHtmlBuilder.getSpans(0, spannedHtml.length(), URLSpan.class);\n        for (final URLSpan span : urls) {\n            setLinkClickable(clickableHtmlBuilder, span);\n        }\n        return clickableHtmlBuilder;\n    }\n\n    \/**\n     * \u8bbe\u7f6e\u70b9\u51fb\u8d85\u94fe\u63a5\u5bf9\u5e94\u7684\u5904\u7406\u5185\u5bb9\n     *\/\n    private void setLinkClickable(final SpannableStringBuilder clickableHtmlBuilder, final URLSpan urlSpan) {\n        int start = clickableHtmlBuilder.getSpanStart(urlSpan);\n        int end = clickableHtmlBuilder.getSpanEnd(urlSpan);\n        int flags = clickableHtmlBuilder.getSpanFlags(urlSpan);\n\n        ClickableSpan clickableSpan = new ClickableSpan() {\n            public void onClick(View view) {\n                Log.i(LianConstants.TAG, &quot;URL: &quot; + urlSpan.getURL());\n\n            }\n        };\n\n        clickableHtmlBuilder.setSpan(clickableSpan, start, end, flags);\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u53ef\u8df3\u8f6c\u53ef\u62e6\u622a\u70b9\u51fb\u4e8b\u4ef6 public class DeepLinkActivity extends AppCom [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[311],"tags":[],"class_list":["post-1878","post","type-post","status-publish","format-standard","hentry","category-android-advance"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1878","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1878"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1878\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}