[英]android: Set link with <a href> in TextView
我动态创建TextView并希望将文本设置为可链接。 文字值为“ Google ”。 我提到了像这样的互联网和博客,它以同样的方式显示,但我无法产生预期的结果。
我尝试了不同的方法,但我看到的输出是仅包含文本的整个文本。 我试过的代码是:
TextView tv1 = new TextView(this);
tv1.setLayoutParams(textOutLayoutParams);
// Make Linkable
tv1.setMovementMethod(LinkMovementMethod.getInstance());
tv1.setText(Html.fromHtml(l.getLeftString()));
/*SpannableString s = new SpannableString(l.getLeftString());
Linkify.addLinks(s, Linkify.WEB_URLS);
tv1.setText(s);
tv1.setMovementMethod(LinkMovementMethod.getInstance());
*/
dialogLayout.addView(tv1);
在我的输出中,我看到“ 谷歌 ”,没有链接。 我也尝试过清洁项目并再次构建它,但没有成功。 我希望只有“Google”标有蓝色下划线(默认情况下),点击Google后,浏览器会打开http://google.com 。
我的代码缺少什么来获得输出? BTW For REF:我使用64位Win 7,Java,Eclipse,Android API 8-2.2
任何帮助都非常感谢。
我终于使用以下代码了解它:
TextView tv1 = new TextView(this);
tv1.setLayoutParams(textOutLayoutParams);
tv1.setText(Html.fromHtml("<a href=\""+ l.getRightString() + "\">" + l.getLeftString() + "</a>"));
tv1.setClickable(true);
tv1.setMovementMethod (LinkMovementMethod.getInstance());
dialogLayout.addView(tv1);
l.getRightString() - 包含一个像http:\\ www.google.com l.getLeftString()的网址 - 包含“Go to Google”等网址的文字
结果:在我的对话框中用蓝色和带下划线的文本“转到谷歌”,点击它后,浏览器打开并显示相应的页面。 在返回/退出浏览器时,它再次从它离开的状态进入应用程序。
希望这可以帮助。
将html保存在字符串中
<string name="link"><a href="http://www.google.com">Google</a></string>
将textview ID设置为
textViewLinkable
在主要活动中使用以下代码:
((TextView) findViewById(R.id.textViewLinkable)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.textViewLinkable)).setText(Html.fromHtml(getResources().getString(R.string.link)));
这是我在Android N上测试的简单实现。
String termsText = "By registering, you are agree to our";
String termsLink = " <a href=https://www.yourdomain.com/terms-conditions.html >Terms of Service</a>";
String privacyLink = " and our <a href=https://www.yourdomain.com/privacy-policy.html >Privacy Policy</a>";
String allText = termsText + termsLink + privacyLink;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
((TextView) findViewById(R.id.text_terms_conditions)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.text_terms_conditions)).setText(Html.fromHtml(allText, Html.FROM_HTML_MODE_LEGACY));
}
else {
((TextView) findViewById(R.id.text_terms_conditions)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.text_terms_conditions)).setText(Html.fromHtml(allText));
}
我也面临着我使用以下解决的相同问题
String str_text = "<a href=http://www.google.com >Google</a>";
TextView link;
link = (TextView) findViewById(R.id.link);
link.setMovementMethod(LinkMovementMethod.getInstance());
link.setText(Html.fromHtml(str_text));
用于将链接颜色更改为蓝色使用
link.setLinkTextColor(Color.BLUE);
txtview.setMovementMethod(LinkMovementMethod.getInstance());
将此语句传递给textview,并在string.xml中将字符串设置为
<string name="txtCredits"> <a href="http://www.google.com"></a></string>
现在将此字符串名称“android:text =”@ string / txtCredits“传递给txtview所在的xml类。
像这样
private String getLink(String string){
LinkExtractor linkExtractor = LinkExtractor.builder()
.linkTypes(EnumSet.of(LinkType.URL)) // limit to URLs
.build();
Iterable<Span> spans = linkExtractor.extractSpans(string);
StringBuilder sb = new StringBuilder();
for (Span span : spans) {
String text = string.substring(span.getBeginIndex(), span.getEndIndex());
if (span instanceof LinkSpan) {
// span is a URL
sb.append("<a href=\"");
sb.append(text);
sb.append("\">");
sb.append(text);
sb.append("</a>");
} else {
// span is plain text before/after link
sb.append(text);
}
}
return sb.toString(); // "wow <a href=\"http://test.com\">http://test.com</a> such linked"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.