TextView中有多个超链接 [英] textview with multiple hyperlinks

查看:212
本文介绍了TextView中有多个超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有例如以下字符串

\\ n关门合唱团是一个HTTP://www.last.fm/tag/post-grunge \\级= \\BB code_tag \\相对= \\标签\\> - 垃圾带后从Escatawpa,密西西比州,美国,成立于1996年,由布拉德·阿诺德(主唱),马特·罗伯茨(吉他),托德·哈勒尔(低音),克里斯·亨德森(吉他)和格雷格·厄普丘奇(鼓手),乐队签约环球记录在2000年的第一张专辑,HTTP://www.last.fm/music/3+Doors+Down/The+Better+Life \\级= \\BB code_album \\>的美好生活。他们收到国际社会的关注与单&QUOT的释放; HTTP://www.last.fm/music/3+Doors+Down/_/Kryptonite \\级= \\BB code_track \\>氪石"专辑继续卖出超过600万张。\\ n \\ n http://www.last.fm/music/3+Doors+Down\\\">Read更多关于关门合唱团在Last.fm. \\ n \\ n \\ n用户提供的文本在Creative Commons可BY-SA许可,也可能是在GNU FDL可用。\\ n

\n 3 Doors Down is a http://www.last.fm/tag/post-grunge\" class=\"bbcode_tag\" rel=\"tag\">post-grunge band from Escatawpa, Mississippi, United States, formed in 1996, consisting of Brad Arnold (vocals), Matt Roberts (guitars), Todd Harrell (bass), Chris Henderson (guitar), and Greg Upchurch (drums). The band signed to Universal Records in 2000 for their first album, http://www.last.fm/music/3+Doors+Down/The+Better+Life\" class=\"bbcode_album\">The Better Life. They received international attention with the release of the single "http://www.last.fm/music/3+Doors+Down/_/Kryptonite\" class=\"bbcode_track\">Kryptonite". The album went on to sell over 6 million copies. \n\n http://www.last.fm/music/3+Doors+Down\">Read more about 3 Doors Down on Last.fm.\n \n \nUser-contributed text is available under the Creative Commons By-SA License and may also be available under the GNU FDL.\n

我要显示在超链接点击一个TextView整个字符串。我也不希望看到实际的URL,只是在地方的url显示的文字。关于这个问题阅读其他职位都建议定义类似于这样一个TextView

I want to show the entire string in a textview with the hyperlinks clickable. I also don't want to see the actual url, just the text that's shown in place of the url. Reading other posts on the subject they all suggest defining a textview similar to this

<TextView
            android:id="@+id/tvArtistOverview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:autoLink="web"
            android:linksClickable="true" />

和TextView的的SetMovementMethod设置为

and setting the SetMovementMethod of the textview to

myTextView.setMovementMethod(LinkMovementMethod.getInstance());

当我按照这些步骤,我的链接是可以点击的,但是我希望他们不会显示。我缺少什么?

When I follow these steps, my links are clickable, however they are not displayed as I wish. What am I missing?

下面是它目前的样子的例子。

Here is an example of how it currently looks.

推荐答案

使用低于code。

TextView tv = ....
tv.setMovementMethod(LinkMovementMethod.getInstance());

    String content = tv.getText().toString();
    List<String> links = new ArrayList<String>();

    Pattern p = Patterns.WEB_URL;
    Matcher m = p.matcher(content);
    while (m.find()) {
        String urlStr = m.group();
        links.add(urlStr);
    }

    SpannableString f = new SpannableString(content);

    for (int i = 0; i < links.size(); i++) {
        final String url = links.get(i);

        f.setSpan(new InternalURLSpan(new OnClickListener() {
            public void onClick(View v) {
                Context ctx = v.getContext();
                String urlToOpen = url;
                if (!urlToOpen.startsWith("http://") || !urlToOpen.startsWith("https://"))
                    urlToOpen = "http://" + urlToOpen;
                openURLInBrowser(urlToOpen, ctx);
            }
        }), content.indexOf(url), content.indexOf(url) + url.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    tv.setText(f);

这篇关于TextView中有多个超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆