Android 中的超链接

发布于 2024-09-08 15:27:33 字数 315 浏览 5 评论 0原文

我有一个问题:如何在普通文本的文本块中放置超链接?我有一些文本,在文本中我提到了一个 URL,我希望该 URL 具有不同的颜色、下划线且可点击。

我知道android中的超链接可以用“Linkify”放置.. 我已经提到了 android docs 页面

考虑相同上面带有“android docs”的段落我想在android中显示......

I have a question: How can I place a hyperlink WITHIN a text-block of normal text? I have some text and in the text I mention a URL and I want this URL to be in a different color, underlined and click-able.

I know hyperlinks in android can be placed with "Linkify"..
and i have referred android docs page

Consider the same above paragraph with "android docs" i want to display in android.....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情徒 2024-09-15 15:27:33

@费多尔....
我发现提供了来自 的链接API 演示

这种方法最终用于在段落文本之间提供链接。

     SpannableString ss = new SpannableString("text4: Click here 
                                                  to dial the phone.");

    ss.setSpan(new StyleSpan(Typeface.ITALIC), 0, 6,
           Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    ss.setSpan(new URLSpan("tel:4155551212"), 13, 17,
           Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    TextView t4 = (TextView) findViewById(R.id.TextView01);
    t4.setText(ss);
    t4.setMovementMethod(LinkMovementMethod.getInstance());

@Fedor....
i have found to give links from API Demos .

this approach is finally be used for giving link in between text of paragraph.

     SpannableString ss = new SpannableString("text4: Click here 
                                                  to dial the phone.");

    ss.setSpan(new StyleSpan(Typeface.ITALIC), 0, 6,
           Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    ss.setSpan(new URLSpan("tel:4155551212"), 13, 17,
           Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    TextView t4 = (TextView) findViewById(R.id.TextView01);
    t4.setText(ss);
    t4.setMovementMethod(LinkMovementMethod.getInstance());
给妤﹃绝世温柔 2024-09-15 15:27:33

看看 TextView 下划线电话号码和超链接

但是我发现 Linkify 没有为您提供您需要的功能。没问题,您可以手动实现。只需添加 ClickableSpan 并处理单击即可。代码示例位于 http://www.orangeapple.org/?p=354

Take a look at that TextView underline phone number and hyperlink

But I can see Linkify doesn't provide you the functionality you need. Not a problem, you can implement it manually. Just add ClickableSpans and handlre click. Code sample here http://www.orangeapple.org/?p=354.

缺⑴份安定 2024-09-15 15:27:33

我知道这是一个老问题,但这是我最近遇到的一个问题,而且我没有找到对我有帮助的 StackOverflow 答案。因此,经过大量的实验,我找到了一个解决方案,并想将其分享给后代。

在 strings.xml 中,使用 HTML 标签和标记:

<string name="example"><![CDATA[I know hyperlinks in android can be placed with "Linkify".. and i have referred <a href="http://developer.android.com/reference/android/text/util/Linkify.html">android docs</a> page]]></string>

在 XML 中,确保不添加任何特殊属性。不要使用 android:autolink

在 Java 代码中,必须使用 fromHtml、Linkify 和 LinkMovementMethod。确保在 LinkMovementMethod 之前调用 Linkify

exampleTextView.setText(Html.fromHtml(getString(R.string.example)));
Linkify.addLinks(exampleTextView, Linkify.WEB_URLS);
exampleTextView.setMovementMethod(LinkMovementMethod.getInstance());

另一个警告:如果您使用这些 HTML 链接,其中链接的显示文本与 URL(Google 和 www.google.com)不匹配,请勿将原始 URL 放入字符串中。所以:

<string name="example"><![CDATA[www.google.com I know hyperlinks in android can be placed with "Linkify".. and i have referred <a href="http://developer.android.com/reference/android/text/util/Linkify.html">android docs</a> page]]></string>

会将“www.google.com”转换为链接,但不会将“android docs”转换为链接。

这对我来说是非常令人沮丧的,因为我尝试了许多不同的组合并遵循了许多在线示例,但在我找到上述正确的神奇组合之前,这些组合都不起作用。希望它对其他人有帮助!

I know this is an old question, but this is a problem I recently struggled with, and I found no StackOverflow answer that helped me. So after extensive experimentation, I found a solution and thought I would share it for posterity.

In your strings.xml, use HTML tags and the markers:

<string name="example"><![CDATA[I know hyperlinks in android can be placed with "Linkify".. and i have referred <a href="http://developer.android.com/reference/android/text/util/Linkify.html">android docs</a> page]]></string>

In your XML, make sure to NOT add any special attributes. Do NOT use android:autolink

In your Java code, you must use fromHtml, Linkify, and LinkMovementMethod. Make sure to call Linkify before LinkMovementMethod

exampleTextView.setText(Html.fromHtml(getString(R.string.example)));
Linkify.addLinks(exampleTextView, Linkify.WEB_URLS);
exampleTextView.setMovementMethod(LinkMovementMethod.getInstance());

Another caveat: if you are using these HTML links, where a link has display text that does not match the URL (Google and www.google.com), do NOT put raw URLs in the string. So:

<string name="example"><![CDATA[www.google.com I know hyperlinks in android can be placed with "Linkify".. and i have referred <a href="http://developer.android.com/reference/android/text/util/Linkify.html">android docs</a> page]]></string>

Will turn "www.google.com" into a link, but will not turn "android docs" into a link.

This was very frustrating for me to figure out, as I tried many different combinations and followed many online examples, none of which worked until I found just the right magic combination described above. Hope it helps someone else!

旧伤还要旧人安 2024-09-15 15:27:33

使用 Spannable 字符串

 private void setTermsAndTextToClickable(TextView tandcTextView, String termsAndCon, String wordTohyperLink, String termsAndConUrl){
    if (TextUtils.isEmpty(termsAndCon)){
        tandcTextView.setVisibility(View.GONE);
        return;
    }

    if (!TextUtils.isEmpty(termsAndCon) && !TextUtils.isEmpty(wordTohyperLink) && !TextUtils.isEmpty(termsAndConUrl)){
        SpannableString ssString = new SpannableString(termsAndCon);
        int startLoc = termsAndCon.indexOf(wordTohyperLink);
        int endLoc = termsAndCon.indexOf(wordTohyperLink) + wordTohyperLink.length();
        ssString.setSpan(new URLSpan(termsAndConUrl),startLoc,endLoc, 0);
        tandcTextView.setText(ssString);
        tandcTextView.setMovementMethod(LinkMovementMethod.getInstance());

        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                // We display a Toast. You could do anything you want here.
                Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT).show();

            }
        };
        ssString.setSpan(clickableSpan, startLoc, endLoc, 0);
    }else{
        tandcTextView.setText(termsAndCon);
    }

}

Use Spannable String

 private void setTermsAndTextToClickable(TextView tandcTextView, String termsAndCon, String wordTohyperLink, String termsAndConUrl){
    if (TextUtils.isEmpty(termsAndCon)){
        tandcTextView.setVisibility(View.GONE);
        return;
    }

    if (!TextUtils.isEmpty(termsAndCon) && !TextUtils.isEmpty(wordTohyperLink) && !TextUtils.isEmpty(termsAndConUrl)){
        SpannableString ssString = new SpannableString(termsAndCon);
        int startLoc = termsAndCon.indexOf(wordTohyperLink);
        int endLoc = termsAndCon.indexOf(wordTohyperLink) + wordTohyperLink.length();
        ssString.setSpan(new URLSpan(termsAndConUrl),startLoc,endLoc, 0);
        tandcTextView.setText(ssString);
        tandcTextView.setMovementMethod(LinkMovementMethod.getInstance());

        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                // We display a Toast. You could do anything you want here.
                Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT).show();

            }
        };
        ssString.setSpan(clickableSpan, startLoc, endLoc, 0);
    }else{
        tandcTextView.setText(termsAndCon);
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文