Html <a> tag not working in Android textview

对着背影说爱祢 提交于 2020-01-01 19:43:10

问题


I'm trying to display JSON results in a textview (that resides within a listview). One of the Results is a URL which has to be displayed as "View results". I'm using the following code to display the URL as "View Results":

        String result = "<a href=\"" + jsonObject.get("url") + "\">" + getString(R.string.hyperlink_text) + "</a>" + "\n";
        bbieResults.put("Result", Html.fromHtml(result));

The related xml layout:

<TextView
    android:id="@+id/list_result"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/list_label"
    android:layout_marginLeft="10dp"
    android:autoLink="web"
    android:linksClickable="true"
    android:textSize="25dp" />

This textview does display "View results" as a label for the URL but I can't actually click it. So how can I make this a clickable hyperlink?

Thanks in advance :)


回答1:


textview.setMovementMethod(LinkMovementMethod.getInstance());
textview.setText(Html.fromHtml(str));



回答2:


android:autoLink="web"
android:linksClickable="true"

This worked for me when the textview was inside a .xml file and inside a listview.



来源:https://stackoverflow.com/questions/9380349/html-a-tag-not-working-in-android-textview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!