How to set a hyperlink in clickable textview? Android Java [duplicate]

五迷三道 提交于 2019-12-12 10:45:47

问题


I have a textView with one URL.. But I don't want to show the whole URL but only a few words like: Click here. And when the textview is clicked.. The application need to open te URL "behind" the words Click here..

For your information: The textView is clickable now. The URL displays correctly. When the URL is clicked, the browser will start, and load the URL correctly.

I only want to change the text of the link what is visible.

EDIT: Everytime the app restart or reload, the url can be different, so it is nog always the same URL.


回答1:


use below code :-

    android:autoLink="web"

like this

<TextView
    android:id="@+id/txt_post_message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:autoLink="web"
    android:text=""
    android:textColor="@color/wall_msg"
    android:textSize="16sp" />



回答2:


Have you tried using html to achieve this? So for example have the following as your text and then setting

String text = "<a href='www.link.com'>Click here</a>";

textView.setText(Html.fromHtml(text));



回答3:


Change the text in your textView to whatever you like. And provide the URL as a variable in your code

EDIT: You say the URL changes: So just save the URL in a variable. Would be interesting how your app receives the new url



来源:https://stackoverflow.com/questions/23755219/how-to-set-a-hyperlink-in-clickable-textview-android-java

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