2016-03-30 196 views
0

我有一个TextViewlistview它可以有2+链接。我已经通过这个SO link来捕获TextView的事件,但这不适用于列表视图。带有可点击链接的Android TextView:如何捕获CustomAdapter ListView中的点击?

这里是我的代码:

getView方法

if(ann.message.contains("<a href=")){ 
    setTextViewHTML(holder.announcement, ann.message); 
} 

方法,使文本可点击

protected void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span) 
{ 
    int start = strBuilder.getSpanStart(span); 
    int end = strBuilder.getSpanEnd(span); 
    int flags = strBuilder.getSpanFlags(span); 
    ClickableSpan clickable = new ClickableSpan() { 
     public void onClick(View view) { 
      // Do something with span.getURL() to handle the link click... 
      Log.i("YES-5.0", span.getURL()); 
     } 
    }; 
    strBuilder.setSpan(clickable, start, end, flags); 
    strBuilder.removeSpan(span); 
} 

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); 
} 

这是我TextView

<TextView 
     android:id="@+id/textViewAnnouncementMessage" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="4dp" 
     android:focusable="false" 
     android:textColor="@android:color/black" 
     android:maxLines="5" 
     android:text="@string/message" 
     android:textSize="16sp"/> 

我试图与下面的TextView的`属性的所有组合”,但仍然没有成功:

android:autoLink="all" 
android:clickable="true" 
android:linksClickable="true" 

回答

0

我解决我的问题,加入以下行调用setTextViewHTML()前:

holder.announcement.setMovementMethod(LinkMovementMethod.getInstance()); 

所以现在我getView()方法样子:

if(ann.message.contains("<a href=")){ 
    holder.announcement.setMovementMethod(LinkMovementMethod.getInstance()); 
    setTextViewHTML(holder.announcement, ann.message); 
} 
0

如果使用XML布局你的TextView并且您可点击的链接都开始HTTP:// IL devrait ETRE suffisant D'utiliser莱attributs suivants:

 <TextView 
      ...  
      android:autoLink="all" 
      android:linksClickable="true" 
      android:textColorLink="@color/colorAccent" /> 

自动链接都意味着所有有电子邮件地址,HTTP链接和电话号码将可以点击。请看官方文档以获取更多详细信息:http://developer.android.com/reference/android/text/util/Linkify.html#ALL

+0

试图用你的解决方案,但厄运。 –

+0

我也想捕获我的textview中的每一个链接 –

+0

这应该工作,如果你的链接是在http格式,也尝试设置android:focusable =“false” – gbaccetta

0

您是否在TextView上设置了移动方法?此外,如果您的链接是作为href标记发布的,则可以使用Html.fromHtml为您构建Spannable。例如:

TextView mView = (TextView) findViewById(R.id.text1); 
mView.setMovementMethod(LinkMovementMethod.getInstance()); 
mView.setText(Html.fromHtml(YOUR_LINK_STRING)); 
+0

是的,但问题是我无法捕捉链接点击'setMovementMethod'。 –

+0

假设您以正确的html格式提供链接文本,Html.fromHtml会为您放置ClickableSpans。 (即在浏览器中打开链接) – Submersed

+0

实际上,我不想在浏览器中打开链接我想在Web视图中打开应用程序内的链接。这就是为什么我想要捕获链接点击。 –