2015-11-19 29 views
1

我在ListView中使用TextView組件。有時我在TextView的文字上有鏈接,我想點擊它們打開瀏覽器。 我的文本中的所有鏈接都有一個標籤:TextView上的自動鏈接網站有錯誤的結果

這是我的文本的一個例子。 This is link

對於目的使用:

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

一切都很好,但如果我把一些文字:

這是一個示例文本。 link.thing

在這種情況下,link.thing被選爲鏈接。 我怎樣才能使點擊鏈接<a></a>標籤之間?

回答

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); 
     } 
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()); 
相關問題