我有以下TextView定义:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/txtCredits"
android:autoLink="web" android:id="@+id/infoTxtCredits"
android:layout_centerInParent="true"
android:linksClickable="true"/>
其中@string/txtCredits是一个字符串资源,包含<a href="some site">链接文本</a>。
Android正在突出显示TextView中的链接,但它们不响应单击。我做错了什么?我必须在我的活动中为TextView设置一个onClickListener像这样简单吗?
它看起来和我定义字符串资源的方式有关。
这行不通:
<string name="txtCredits"><a href="http://www.google.com">Google</a></string>
但这确实是:
<string name="txtCredits">www.google.com</string>
这是一个遗憾,因为我宁愿显示一个文本链接,而不是显示完整的URL。
我添加了这一行到TextView: android:autoLink="web"
下面是布局文件中的一个使用示例。
layout.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/txtDefaultpassword"
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>
理查德,下次,你应该在布局XML的TextView下添加这段代码。
android:autoLink="all"
应该是这样的。
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txtCredits"
android:id="@+id/infoTxtCredits"
android:autoLink="all"
android:linksClickable="true">
</TextView>
您不需要使用此代码(t2.setMovementMethod(LinkMovementMethod.getInstance());)来使链接可单击。
另外,这里有一个事实:只要你设置了autoLink和linksClickable,不要忘记在String.xml文件中添加这个,这样可点击的链接就可以工作了。
<string name="txtCredits"><a href="http://www.google.com">Google</a></string>
在API演示中,我找到了问题的解决方案:
文件Link.java:
// text2 has links specified by putting <a> tags in the string
// resource. By default these links will appear but not
// respond to user input. To make them active, you need to
// call setMovementMethod() on the TextView object.
TextView t2 = (TextView) findViewById(R.id.text2);
t2.setMovementMethod(LinkMovementMethod.getInstance());
我删除了我的TextView上的大部分属性,以匹配演示中的内容。
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txtCredits"/>
这就解决了问题。它很难发现和修复。
重要:不要忘记删除autoLink="web"如果你正在调用setMovementMethod()。
上面的解决方案对我没用,但是下面的解决方案对我有用(而且看起来更简洁)。
首先,在字符串资源中,使用HTML实体编码定义标签开头的v形符号,即:
<a href="http://www.google.com">Google</a>
而不是:
<a href="http://www.google.com">Google</a>
一般来说,像这样编码字符串中的所有v形符号。顺便说一下,链接必须以http://开头
然后(正如这里所建议的)在你的TextView上设置这个选项:
android:linksClickable="true"
最后,在代码中,执行以下操作:
((TextView) findViewById(R.id.your_text_view)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.your_text_view)).setText(Html.fromHtml(getResources().getString(R.string.string_with_links)));
就是这样。不需要正则表达式或其他手工操作。
你可以简单地添加链接到你的TextView与Android的Linkify库。
添加到你的strings.xml
<string name="text_legal_notice">By continuing, you confirm that you have read, understood and agreed to our %1$s and %2$s.</string>
<string name="text_terms_conditions">Terms & Conditions</string>
<string name="text_privacy_policy">Privacy Policy</string>
加入到你的活动中
final String termsConditionsText = getString(R.string.text_terms_conditions);
final String privacyPolicyText = getString(R.string.text_privacy_policy);
final String legalText = getString(
R.string.text_legal_notice,
termsConditionsText,
privacyPolicyText
);
viewBinding.textViewLegalNotice.setText(legalText);
Linkify.addLinks(
viewBinding.textViewLegalNotice,
Pattern.compile(termsConditionsText),
null,
null,
(match, url) -> "https://policies.google.com/terms"
);
Linkify.addLinks(
viewBinding.textViewLegalNotice,
Pattern.compile(privacyPolicyText),
null,
null,
(match, url) -> "https://policies.google.com/privacy"
);