Creating a hyperlink within a textview inside a fragment

江枫思渺然 提交于 2019-12-12 04:14:05

问题


I have looked through many of the threads on this site to find the solution to my problem but none seem to work. At the moment I am able to create a hyperlink while displaying the website eg - Please go here - www.google.com (The www.google.com is hyperlinked). What I am trying to do is have the link as - Please go here (Here is the link). Below is the code i have tried but once I remove the link itself, the 'here' still highlights like a link but has no function.

Strings.xml:

<string name="goog">Please go <a href="www.google.com">here</a></string>

fragment_home.xml:

 <TextView
    android:id="@+id/npd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:autoLink="web"
    android:fontFamily="sans-serif-light"
    android:linksClickable="true"
    android:text="@string/npd"
    android:textColorLink="#FF0000"
    android:textSize="12dp" />

If i change 'here' for www.google.com within Strings.xml, the link takes me directly there. Has anyone been able to figure this problem out yet? Thanks in advance.


回答1:


Try this and let me know is it what you are looking for

TextView textView =(TextView)findViewById(R.id.textView);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='http://www.google.com'> Google </a>";
textView.setText(Html.fromHtml(text));


来源:https://stackoverflow.com/questions/24403987/creating-a-hyperlink-within-a-textview-inside-a-fragment

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