android:在 TextView 中使用 <a href> 设置链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12119800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android: Set link with <a href> in TextView
提问by Tvd
I create a TextView dynamically and want to set the text as linkable. Text value is "Google". I referred to internet & blogs like this,which shows the same way, but I couldn't produce the expected results.
我动态创建一个 TextView 并希望将文本设置为可链接。文本值为“谷歌”。我参考了这样的互联网和博客,显示方式相同,但我无法产生预期的结果。
I tried different ways, but the output I see is the whole text with text only. The code I have tried with is :
我尝试了不同的方法,但我看到的输出是整个文本,只有文本。我试过的代码是:
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);
In my output I see "Google" and no link. I also tried Clean project & building it again, but no success. I am looking to see only "Google" as underlined with blue color (as default) and on clicking Google, the browser open with http://google.com.
在我的输出中,我看到“ Google”但没有链接。我也尝试过 Clean 项目并再次构建它,但没有成功。我希望只看到带有蓝色下划线的“Google”(默认),点击 Google 时,浏览器会打开http://google.com。
What is lacking in my code to get the output ? BTW For REF : I use 64bit Win 7, Java, Eclipse, Android API 8-2.2
我的代码缺少什么来获取输出?顺便说一句,对于 REF:我使用 64 位 Win 7、Java、Eclipse、Android API 8-2.2
Any help is highly appreciated.
任何帮助都受到高度赞赏。
回答by Tvd
I finally got it working using the following code :
我终于使用以下代码让它工作了:
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() - contains a url like http:\www.google.com l.getLeftString() - contains text for the url like "Go to Google"
l.getRightString() - 包含类似 http:\www.google.com 的 url l.getLeftString() - 包含像“Go to Google”这样的 url 文本
RESULTS : Text "Go to Google" on my dialog with blue color and underlined, and on clicking it the browser opens up and shwows the respective page. On returning/Exiting the browser it again comes to the app from the state where it had left.
结果:我的对话框中带有蓝色和下划线的文本“Go to Google”,单击它时,浏览器会打开并显示相应的页面。在返回/退出浏览器时,它再次从离开的状态进入应用程序。
Hope this helps.
希望这可以帮助。
回答by SquiresSquire
Save your html in a string
将您的 html 保存在字符串中
<string name="link"><a href="http://www.google.com">Google</a></string>
Set textview ID to to
将文本视图 ID 设置为
textViewLinkable
In main activity use following code:
在主要活动中使用以下代码:
((TextView) findViewById(R.id.textViewLinkable)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.textViewLinkable)).setText(Html.fromHtml(getResources().getString(R.string.link)));
回答by user2837615
Here is my simple implementation tested up to Android N.
这是我测试到 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));
}
回答by Ganesh Jogam
I was also facing the same issue I resolved using following
我也面临着我使用以下解决的相同问题
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));
for changing the link color to blue use
用于将链接颜色更改为蓝色使用
link.setLinkTextColor(Color.BLUE);
回答by Naina
txtview.setMovementMethod(LinkMovementMethod.getInstance());
Pass this statement to your textview, and in string.xml set an string as
将此语句传递给您的文本视图,并在 string.xml 中将字符串设置为
<string name="txtCredits"> <a href="http://www.google.com"></a></string>
Now pass this string name " android:text="@string/txtCredits" to your xml class where the txtview is there .
现在将此字符串名称“android:text="@string/txtCredits”传递给 txtview 所在的 xml 类。
回答by ibrahim
use this code autolink-java On GitHub
like this
像这样
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"
}