单击TextView中的链接

时间:2013-06-18 12:15:02

标签: android textview

我在TextView中有一些带有链接的文本。现在我希望浏览器打开链接,如果用户点击它。我的TextView看起来像这样:

<string name="Info">Go to <a href="www.google.com">Google</a></string>

<TextView
        android:id="@+id/Info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/OptionMarginBottom"
        android:autoLink="web"
        android:linksClickable="true"
        android:text="@string/Info" />

链接以蓝色显示正确但我无法点击它。那是为什么?

4 个答案:

答案 0 :(得分:2)

使用此方法

public static void addLink(TextView textView, String patternToMatch,
        final String link) {
    Linkify.TransformFilter filter = new Linkify.TransformFilter() {
        @Override public String transformUrl(Matcher match, String url) {
            return link;
        }
    };
    Linkify.addLinks(textView, Pattern.compile(patternToMatch), null, null,
            filter);
}

并用作

addLink(text, "^Android", "http://abhiandroidinfo.blogspot.in");

答案 1 :(得分:1)

Linkify

上使用TextView
Linkify.addLinks(yourTextviewObject, Linkify.WEB_URLS);
  

Linkify采取一段文字和一个正则表达式并将其全部转换   正则表达式在文本中匹配为可点击链接。

答案 2 :(得分:0)

帮手方法:

public static void addLinks(TextView textView, String linkThis, String toThis) {
        Pattern pattern = Pattern.compile(linkThis);
        String scheme = toThis;
        android.text.util.Linkify.addLinks(textView, pattern, scheme, new MatchFilter() {
            @Override
            public boolean acceptMatch(CharSequence s, int start, int end) {
                return true;
            }
        }, new TransformFilter() {

            @Override
            public String transformUrl(Matcher match, String url) {
                return "";
            }
        });
    }

现在使用如下:

String weblink = "WebsiteName";
String url = course.getString(TAG_Info);

TextView txtInfo= (TextView) findViewById(R.id.Info);
txtInfo.setText(userCanSeeThis);

addLinks(txtInfo, weblink, url);

答案 3 :(得分:0)

更简单的方法是创建一个字符串并添加链接文本 并在xml文件中,生成textView:

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:autoLink="web"
    android:text="@string/string_with_link" />
相关问题