1

我正在使用一个TextView组件ListView。有时我的文本中有一个链接,我TextView想通过单击它们来打开浏览器。我文本中的所有链接都有一个标签:

这是我的文字的一个例子。这是链接

为此,我使用:

textView.setText(Html.fromHtml(someTextString)); textView.setMovementMethod(LinkMovementMethod.getInstance()); textView.setAutoLinkMask(Linkify.WEB_URLS); textView.setLinksClickable(true);

一切都很好,但是如果我输入一些文字:

这是一个示例文本。链接的东西

在那种情况下link.thing被选为链接。如何在<a></a>标签之间制作可点击的链接?

4

2 回答 2

1

添加

android:text="@string/Your_String_Contain"

现在这起着至关重要的作用

<string name="Your_String_Contain">This is an example of my text  <a href="http://www.yourlink.com">This is link</a></string>

然后打电话setMovementMethod

TextView Tv_App_Link=(TextView)findViewById(R.id.Your_Textview_Id);
Tv_App_Link.setMovementMethod(LinkMovementMethod.getInstance());
于 2015-11-19T08:38:24.200 回答
0

这样你可以设置链接它会自动找到链接

  String myHtmlStr = "<a href=www.google.com>click here</a>";

            setTextViewHTML(myTextView, myHtmlStr);

你可以实现这个方法来实现这个

 protected void setTextViewHTML(TextView text, String html) {
            CharSequence sequence = Html.fromHtml(html);
            SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
            URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);
            for (URLSpan span : urls) {
                makeLinkClickable(strBuilder, span);
            }
            text.setText(strBuilder);
        } 

protected void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span) {

            int start = strBuilder.getSpanStart(span);
            int end = strBuilder.getSpanEnd(span);
            int flags = strBuilder.getSpanFlags(span);
            TouchableSpan touchableSpan = new TouchableSpan() {

                @Override
                public void onClick(View widget) {

                  //your logic
                }
            };
            touchableSpan.setURLSpan(span);
            strBuilder.setSpan(touchableSpan, start, end, flags);
            strBuilder.removeSpan(span);
        }
于 2015-11-19T08:33:10.543 回答