Android: How can I add HTML links inside a ListView?

后端 未结 5 688
旧时难觅i
旧时难觅i 2020-12-04 23:19

How would I go about adding clickable links inside a ListView?

5条回答
  •  半阙折子戏
    2020-12-04 23:45

    If you have text that is already in HTML format, the best thing to do is the following:

    TextView textcontent = (TextView) findViewById(...);
    textcontent.setMovementMethod(LinkMovementMethod.getInstance());
    
    String text = "stackoverflow.com";
    textcontent.setText(Html.fromHtml(text));
    

    This will cause any link tags to be clickable in your text view. Alternately, you could use android:autoLink="web" as suggested by Legend, but this has the side-effect of a) linkifying urls that are not wrapped in anchor tags, and b) potentially missing urls or linkifying things that aren't urls. If you want the smarts of autoLink then you should use it, but if all you want is to linkify the tags that are already there, you're better off using setMovementMethod().

    See this bug report for more details: http://code.google.com/p/android/issues/detail?id=2219

提交回复
热议问题