回答
你可以有两个独立的TextViews,你可以在你的布局,如果需要相应地调整他们:
Text1.setText(
Html.fromHtml(
"<a href=\"http://www.google.com\">google</a> "));
Text1.setMovementMethod(LinkMovementMethod.getInstance());
Text2.setText(
Html.fromHtml(
"<a href=\"http://www.stackoverflow.com\">stackoverflow</a> "));
Text2.setMovementMethod(LinkMovementMethod.getInstance());
然后,如果你想要去除的“链接下划线”。创建一个类:
public class URLSpanNoUnderline extends URLSpan {
public URLSpanNoUnderline(String url) {
super(url);
}
@Override public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
}
然后在你的主Activity类,你有TextViews
private void stripUnderlines(TextView textView) {
Spannable s = new SpannableString(textView.getText());
URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
for (URLSpan span: spans) {
int start = s.getSpanStart(span);
int end = s.getSpanEnd(span);
s.removeSpan(span);
span = new URLSpanNoUnderline(span.getURL());
s.setSpan(span, start, end, 0);
}
textView.setText(s);
}
然后就是把这个你初始化的TextViews后(在你的onCreate)添加这个方法:
stripUnderlines(Text1);
stripUnderlines(Text2);
注不起作用:这不是** **工作,如果你设置你的TextView的属性自动链接到一些属性!如果你有,你必须在调用setText() – Xebozone 2015-12-21 03:40:10
这应该工作。
TextView t2 = (TextView) findViewById(R.id.text2);
t2.setMovementMethod(LinkMovementMethod.getInstance());
and
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/txtCredits"
android:id="@+id/text2"
android:layout_centerInParent="true"
android:layout_marginTop="20dp"></TextView>
但我不想显示整个链路 – 2012-03-24 13:59:52
是和对于您可以包括这在你的字符串资源 <字符串名称=“txtCredits”>Google。 – 2012-03-24 14:16:29
我试试看!谢谢! – 2012-03-24 14:17:17
很简单的方法来做到这一点---
在你Activity--
TextView tv = (TextView) findViewById(R.id.site);
tv.setText(Html.fromHtml("<a href=http://www.stackoverflow.com> STACK OVERFLOW "));
tv.setMovementMethod(LinkMovementMethod.getInstance());
然后你会得到公正的标签,而不是整个链接..
希望它会帮助你...
TextView t2 = (TextView) findViewById(R.id.textviewidname);
t2.setMovementMethod(LinkMovementMethod.getInstance());
和
<string name="google_stackoverflow"><a href="https://stackoverflow.com/questions/9852184/android-textview-hyperlink?rq=1">google stack overflow</a></string>
链接是“Android: textview hyperlink”
和标记是“谷歌堆栈溢出”
定义您的Java中的第一个代码块和您的strings.xml文件中的第二个代码块。另外,请确保在您的java中引用来自页面布局的textView的id。
之前调用'textView.setAutoLinkMask(0);'我最喜欢你的方式。恕我直言,这是最干净的。 – plackemacher 2012-07-23 21:19:19
我很高兴能为您服务。 – epicness42 2012-07-24 00:16:11
这是我的工作实现
private void showMessage()
{
lblMessage.setText("");
List<String> messages = db.getAllGCMMessages();
for (int k = messages.size() - 1; k >= 0; --k)
{
String message = messages.get(k).toString();
lblMessage.append(message + "\n\n");
}
Linkify.addLinks(lblMessage, Linkify.ALL);
}
,并改变超链接的颜色,我editted我的XML来的TextView -
android:textColorLink="#69463d"
我击中了同样的问题,并最终找到了可行的解决方案。
在字符串中。xml文件,定义:
<string name="textWithHtml">The URL link is <a href="http://www.google.com">Google</a></string>
替换 “<” 小于字符的HTML转义字符。
在Java代码:
String text = v.getContext().getString(R.string.textWithHtml); textView.setText(Html.fromHtml(text)); textView.setMovementMethod(LinkMovementMethod.getInstance());
然后将文本框会正确显示与点击的锚链接的文本
android:autoLink="web"
只是工作,如果你有HTML中的完整链接。以下内容将以蓝色和可点击的形式突出显示:
- 1. 超级链接
- 2. 安卓:多TextView的
- 3. TextView超链接不工作?
- 4. 安卓textview环绕textview
- 5. 从超级链接
- 6. 从超级链接
- 7. 的超级链接标签
- 8. 冻结的TextView +安卓
- 9. 的TextView与多个超链接
- 10. Android TextView中的内联超链接
- 11. 在android中的textview超链接
- 12. 超级链接列表colums
- 13. ms超级链接饼图
- 14. 使用超级链接
- 15. 从方法(安卓)Chaning TextView
- 16. 如何textview和安卓
- 17. Click on TextView超链接时出错
- 18. 插入超链接,并删除超级链接在Javascript
- 19. 直接超级链接组成
- 20. 安卓:共享一个链接至Facebook
- 21. 安卓:带有可点击HTML链接
- 22. jQuery的超级链接特性
- 23. 安卓的TextView没有滚动自动
- 24. TextView中是不可见的安卓5.1.1
- 25. 安卓:TextView的集y坐标动态
- 26. 安卓:添加边框的TextView
- 27. 安卓:Java的:TextView中不会变色
- 28. TextView中的链接
- 29. 安卓:我的TextView如何把与TextView的
- 30. ASP.NET“到底部”超级链接
看看这个:http://stackoverflow.com/a/2746708/646391 – Emran 2012-03-24 13:44:34
可能的重复[如何使TextView中的链接可点击?](http:/ /stackoverflow.com/questions/2734270/how-do-i-make-links-in-a-textview-clickable) – CAMOBAP 2014-12-11 15:17:15
这里有几个问题的答案打电话Html.fromHtml,并且要么不为超文本使用的strings.xml或混淆XML中的超文本(例如,“<”替换XML文件中的“<”)。这不必要地使事情复杂化。 AFAICT,最正确的答案应该是直接在strings.xml中键入HTML锚标记,请确保TextView的自动链接属性没有设置,然后使用setMovementMethod设置LinkMovementMethod。有几个答案已经显示如何做到这一点。 – 2015-11-06 22:53:49