2012-02-15 108 views
57

我想打一个链接,像Google一个TextView文本超链接的TextView。反正有这样的链接。 (即)当点击Google这个词时,它应该打开适当的链接。任何想法都欢迎。让android系统

+0

重复:http://stackoverflow.com/questions/2116162/how-to-display-html -in-textview – m0skit0 2012-02-15 09:21:16

+0

http:// stackoverflow。com/questions/3204036/hyperlink-in-android – 2012-02-15 09:23:58

+0

只需使用Linkify.addLinks(TextView,Linkify.ALL); – Nepster 2014-07-14 06:26:34

回答

96

试试这个,让我知道发生什么事..

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)); 
+0

我得到了预期的输出。感谢您的回答 – Srinivas 2012-02-15 09:43:55

+2

它也可以通过使用android:autoLink =“email” – 2014-05-23 13:21:06

42

使用android:autoLink="web"在你的TextView的XML。它应该会自动转换网址,点击能(如果在文本中找到)

+13

还包括android:linksClickable =“true” – 2013-08-20 11:59:02

+0

@waqaslam在这种情况下,我们把一个链接。 – 2016-05-05 13:40:35

+1

@ Garg's这是您在textview上设置的文本,可能包含超链接 – waqaslam 2016-05-05 17:53:23

25

所有测试和
解决方案的工作100%:低于android:autoLink="web"
是一个完整的例子

样品布局的Xml

<TextView 
     android:id="@+id/txtLostpassword" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:autoLink="email" 
     android:gravity="center" 
     android:padding="20px" 
     android:text="@string/lostpassword" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <TextView 
     android:id="@+id/txtLostpassword" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:autoLink="web" 
     android:gravity="center" 
     android:padding="20px" 
     android:text="@string/defaultpassword" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

在string.xml字符串

<string name="lostpassword">If you lost your password please contact <a href="mailto:[email protected]?Subject=Lost%20Password" target="_top">[email protected]</a></string> 

<string name="defaultpassword">User Guide <a href="http://www.cleverfinger.com.au/user-guide/">http://www.cleverfinger.com.au/user-guide/</a></string> 
+3

它不起作用,如果我更改此行> http://www.cleverfinger.com.au/user-guide/一个不同的文字,如:点击这里 – Daniel 2014-04-15 16:57:44

+0

不适合我。 – 2015-07-27 09:50:54

+2

只有当文本包含完整的网址时才有效:www.something.com – 2015-08-12 12:57:52

5

这也通过使用默认属性的TextView

android:autoLink="email" 
2

注: - Html.fromHtml采用的是Androidñ弃用

你需要做的检查和支持Android N和更高VERS Android的

    //Set clickable true 
       tagHeading.setClickable(true); 

        //Handlle click event 
        tagHeading.setMovementMethod(LinkMovementMethod.getInstance()); 

       if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { 
        tagHeading.setText(Html.fromHtml("<a href='https://github.com/hiteshsahu'>https://github.com/hiteshsahu</a>", Html.FROM_HTML_MODE_LEGACY)); 
       } else { 
        tagHeading.setText(Html.fromHtml("<a href='https://github.com/hiteshsahu'>https://github.com/hiteshsahu</a>")); 
       } 

离子或者

你可以不想ID编程上的TextView添加自动链接标志。

机器人:自动链接= “网络”

机器人:linksClickable = “真”

这样,你并不需要添加<a href='somelink'>标签。

这是一个缺点,如果你想在text上添加hyperlink你不能这样做。从两个途径[hiteshsahu] [1]

  <TextView 
       android:id="@+id/tag_info" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/tag_ll" 
       android:layout_gravity="left" 
       android:layout_margin="10dp" 
       android:autoLink="web" 
       android:linksClickable="true" 
       android:text="https://github.com/hiteshsahu" 
       android:textColor="@color/secondary_text" /> 

结果: - - :例如,你不能做这样的事情

https://github.com/hiteshsahu

1

对于SDK fromHtml的最新版本已经过时 使用下面一行

String yourtext = "<a style='text-decoration:underline' href='http://www.sample.com'> Sample Website </a>"; 
    if (Build.VERSION.SDK_INT >= 24) { 
     textView.setText(Html.fromHtml(yourtext, Html.FROM_HTML_MODE_LEGACY)); 
    } else { 
     textView.setText(Html.fromHtml(yourtext)); 
    }