将值传递给其中包含 link 的 string.xml 值
pass value to string.xml value that have a link inside it
我在 string.xml 文件中有这个值:
<string name="rights">Copyright © All Rights Reserved %1$d <b> <a href="http://www.example.com/">company name</a></b></string>
并且我应用了传递给上述文本的代码并将该文本设置为 TextView
private void setupAppInfoRights() {
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
String rights = String.format(new Locale("en"), getString(R.string.rights), currentYear);
appInfoRights.setText(rights);
appInfoRights.setMovementMethod(LinkMovementMethod.getInstance());
}
当我删除传递的值时,一切正常&当用户点击公司名称时,它 him/her 到公司网站。
请注意,我在 xml 中尝试过自动链接,但没有传递任何值,但它没有按预期工作。
但是,当我添加传递的值并使用公司名称上方的代码时没有下划线,当用户单击它时,它什么也不会做。
如何编辑我的上述代码以传递当前年份并保持 link 行为正常?
注意:我已经使用 String.format
将当前年份显示为英语数字,尽管其他语言环境数字仍然如此。
我想 SpannableStringBuilder
就是您要找的。
TextView linkTv = (TextView) findViewById(R.id.link_tv);
linkTv.setMovementMethod(LinkMovementMethod.getInstance());
Spannable span = (Spannable) linkTv.getText();
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget){
//open the link
}
};
span.setSpan(clickableSpan, 0, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//for bold
span.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), 0, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
如果您只想使某些部分可点击,则切换 0
和 span.length()
中的值 setSpan()
。
当您拥有同时具有格式参数(如 %1$d
)和 html 标记的字符串资源时,您必须使用多步骤过程来创建样式化的 CharSequence
。这项额外的工作是必要的,因为 Resources.getString(int, Object...)
和 String.format(String, Object...)
都只能 return String
实例,而不是其他能够保存样式信息的 CharSequence
子类。
首先,将您的字符串资源更改为使用 html 实体来转义 html 标签:
<string name="rights">Copyright © All Rights Reserved %1$d <b> <a href="http://www.example.com/">company name</a></b></string>
接下来,获取一个 String
,格式参数替换为您想要的实际值:
String withHtmlMarkup = getString(R.string.rights, currentYear);
最后,使用Html.fromHtml()
解析html标记:
CharSequence styled = Html.fromHtml(withHtmlMarkup);
然后您可以像往常一样将此 styled
文本设置到您的 TextView:
appInfoRights.setText(styled);
appInfoRights.setMovementMethod(LinkMovementMethod.getInstance());
开发者指南:https://developer.android.com/guide/topics/resources/string-resource#FormattingAndStyling
Normally, this doesn't work because the format(String, Object...)
and getString(int, Object...)
methods strip all the style information from the string. The work-around to this is to write the HTML tags with escaped entities, which are then recovered with fromHtml(String)
, after the formatting takes place.
我在 string.xml 文件中有这个值:
<string name="rights">Copyright © All Rights Reserved %1$d <b> <a href="http://www.example.com/">company name</a></b></string>
并且我应用了传递给上述文本的代码并将该文本设置为 TextView
private void setupAppInfoRights() {
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
String rights = String.format(new Locale("en"), getString(R.string.rights), currentYear);
appInfoRights.setText(rights);
appInfoRights.setMovementMethod(LinkMovementMethod.getInstance());
}
当我删除传递的值时,一切正常&当用户点击公司名称时,它 him/her 到公司网站。
请注意,我在 xml 中尝试过自动链接,但没有传递任何值,但它没有按预期工作。
但是,当我添加传递的值并使用公司名称上方的代码时没有下划线,当用户单击它时,它什么也不会做。
如何编辑我的上述代码以传递当前年份并保持 link 行为正常?
注意:我已经使用 String.format
将当前年份显示为英语数字,尽管其他语言环境数字仍然如此。
我想 SpannableStringBuilder
就是您要找的。
TextView linkTv = (TextView) findViewById(R.id.link_tv);
linkTv.setMovementMethod(LinkMovementMethod.getInstance());
Spannable span = (Spannable) linkTv.getText();
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget){
//open the link
}
};
span.setSpan(clickableSpan, 0, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//for bold
span.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), 0, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
如果您只想使某些部分可点击,则切换 0
和 span.length()
中的值 setSpan()
。
当您拥有同时具有格式参数(如 %1$d
)和 html 标记的字符串资源时,您必须使用多步骤过程来创建样式化的 CharSequence
。这项额外的工作是必要的,因为 Resources.getString(int, Object...)
和 String.format(String, Object...)
都只能 return String
实例,而不是其他能够保存样式信息的 CharSequence
子类。
首先,将您的字符串资源更改为使用 html 实体来转义 html 标签:
<string name="rights">Copyright © All Rights Reserved %1$d <b> <a href="http://www.example.com/">company name</a></b></string>
接下来,获取一个 String
,格式参数替换为您想要的实际值:
String withHtmlMarkup = getString(R.string.rights, currentYear);
最后,使用Html.fromHtml()
解析html标记:
CharSequence styled = Html.fromHtml(withHtmlMarkup);
然后您可以像往常一样将此 styled
文本设置到您的 TextView:
appInfoRights.setText(styled);
appInfoRights.setMovementMethod(LinkMovementMethod.getInstance());
开发者指南:https://developer.android.com/guide/topics/resources/string-resource#FormattingAndStyling
Normally, this doesn't work because the
format(String, Object...)
andgetString(int, Object...)
methods strip all the style information from the string. The work-around to this is to write the HTML tags with escaped entities, which are then recovered withfromHtml(String)
, after the formatting takes place.