Html tag not working in Android textview

2019-04-15 17:39发布

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 :)

2条回答
神经病院院长
2楼-- · 2019-04-15 18:27
android:autoLink="web"
android:linksClickable="true"

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

查看更多
我只想做你的唯一
3楼-- · 2019-04-15 18:37
textview.setMovementMethod(LinkMovementMethod.getInstance());
textview.setText(Html.fromHtml(str));
查看更多
登录 后发表回答