【发布时间】:2012-03-06 15:12:08
【问题描述】:
我想为 Google 之类的 textview 文本创建一个链接。反正有没有这样的链接。 (即)点击 Google 一词时,应打开相应的链接。欢迎任何想法。
【问题讨论】:
-
只需使用 Linkify.addLinks(TextView, Linkify.ALL);
标签: android url hyperlink textview
我想为 Google 之类的 textview 文本创建一个链接。反正有没有这样的链接。 (即)点击 Google 一词时,应打开相应的链接。欢迎任何想法。
【问题讨论】:
标签: android url hyperlink textview
试试这个,让我知道会发生什么..
使用java代码:
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));
从 API 级别 >= 24 开始,Html.fromHtml(String source) 已弃用,而使用 fromHtml(String, int),
textView.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT));
或者在布局 xml 文件中,在 TextView 小部件属性中
android:autoLink="web"
android:linksClickable="true"
【讨论】:
android:autoLink="web"。我两者都有,但没有用。
<![CDATA[string]>> 包围字符串,其中字符串是带有 ulr 的整个字符串文本。试图解决这个问题一个小时......
在 TextView 的 xml 中使用 android:autoLink="web"。它应该自动转换可点击的网址(如果在文本中找到)
【讨论】:
所有测试和工作 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:support@cleverfinger.com.au?Subject=Lost%20Password" target="_top">support@cleverfinger.com.au</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>
【讨论】:
这也可以通过使用Textview的默认属性来完成
android:autoLink="email"
【讨论】:
注意:- Html.fromHtml 在 Android N 中已弃用
您需要检查并支持Android N及更高版本的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>"));
}
或者
您可能不想以编程方式在 TextView 上添加 autoLink 标志。
android:autoLink="web"
android:linksClickable="true"
这样就不用加<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));
}
【讨论】:
我做了一个textview的以下扩展功能。
@SuppressLint("SetTextI18n")
fun TextView.makeHyperLink(url: String) {
text = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Html.fromHtml("<a href='${url}'>${text}</a>", Html.FROM_HTML_MODE_COMPACT)
} else {
Html.fromHtml("<a href='${url}'>${text}</a>")
}
movementMethod = LinkMovementMethod.getInstance()
isClickable = true
}
这样称呼它
tvTos.makeHyperLink(tos_url)
您也可以将文本作为参数传递。
【讨论】: