我想打一个链接,像Google一个TextView文本超链接的TextView。反正有这样的链接。 (即)当点击Google这个词时,它应该打开适当的链接。任何想法都欢迎。让android系统
回答
试试这个,让我知道发生什么事..
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));
我得到了预期的输出。感谢您的回答 – Srinivas 2012-02-15 09:43:55
它也可以通过使用android:autoLink =“email” – 2014-05-23 13:21:06
使用android:autoLink="web"
在你的TextView的XML。它应该会自动转换网址,点击能(如果在文本中找到)
还包括android:linksClickable =“true” – 2013-08-20 11:59:02
@waqaslam在这种情况下,我们把一个链接。 – 2016-05-05 13:40:35
@ Garg's这是您在textview上设置的文本,可能包含超链接 – waqaslam 2016-05-05 17:53:23
所有测试和
解决方案的工作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>
它不起作用,如果我更改此行> http://www.cleverfinger.com.au/user-guide/一个不同的文字,如:点击这里 – Daniel 2014-04-15 16:57:44
不适合我。 – 2015-07-27 09:50:54
只有当文本包含完整的网址时才有效:www.something.com – 2015-08-12 12:57:52
这也通过使用默认属性的TextView
做android:autoLink="email"
注: - 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" />
结果: - - :例如,你不能做这样的事情
对于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));
}
- 1. alertbox android系统
- 2. 在android系统
- 3. android系统
- 4. 在android系统
- 5. 在android系统
- 6. 在android系统
- 7. android系统
- 8. android系统
- 9. 在android系统
- 10. android系统
- 11. android系统
- 12. android系统
- 13. android系统
- 14. android系统
- 15. android系统
- 16. android系统
- 17. android系统
- 18. android系统
- 19. android系统xamarin
- 20. NoSuchMethodException android系统
- 21. 在android系统
- 22. android系统
- 23. android系统
- 24. android系统
- 25. 在android系统
- 26. android系统
- 27. android系统
- 28. android系统
- 29. MultiselectListpreference android系统
- 30. android系统
重复:http://stackoverflow.com/questions/2116162/how-to-display-html -in-textview – m0skit0 2012-02-15 09:21:16
http:// stackoverflow。com/questions/3204036/hyperlink-in-android – 2012-02-15 09:23:58
只需使用Linkify.addLinks(TextView,Linkify.ALL); – Nepster 2014-07-14 06:26:34