gpt4 book ai didi

Android格式化xml字符串丢失html标签

转载 作者:太空宇宙 更新时间:2023-11-03 12:17:36 24 4
gpt4 key购买 nike

我下面有一个 xml 字符串,其中包含 html 标记,例如粗体和超链接 href。

<string name"example"><b>%1$s</b> has been added to your clipboard  and is valid until  <b>%2$s</b><a href="http://www.google.com">Please see our +T\'s and C\'s+ </a>   </string>

当我格式化字符串以动态添加一些值时,它会删除我定义的粗体文本和 href。

代码如下:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yy");
Date date = new Date(text.getValidTo());
String text = String.format(getString(R.string_dialog_text), text.getCode(), simpleDateFormat.format(date), "£30");

然后我将它应用到一个警告对话框

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setNeutralButton(R.string.ok, onClickListener);
builder.setTitle(title);
builder.setMessage(text);

如果我不格式化文本并直接使用字符串资源,这工作得很好

最佳答案

使用Html.fromHtml

builder.setMessage(Html.fromHtml(text));

当您应用格式时,CharSequence转换回 String , 你需要 Spannable带有 html 信息。

来自文档:

Sometimes you may want to create a styled text resource that is also used as a format string. Normally, this won't work because the String.format(String, Object...) method will 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.

尝试使用 &lt;b>代替 <b>&lt;/b>就位</b>

关于Android格式化xml字符串丢失html标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29343192/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com