Android 中持续存在于字符串替换中的下划线文本

Underlining text in Android that persists across a string replace

this question 的答案非常适合我的文本...第一次。我有一些资源文件定义的字符串:

<string name="permission_replace">PERMISSION</string>
<string name="warn_permission">This app needs PERMISSION to work properly. Update in <u>settings</u>.</string>

<u> 标签在我第一次显示文本时工作正常,但是当我去替换我的占位符 ("PERMISSION") 时,下划线丢失了。这是我进行替换的方法:

warnPermissionText.setText(warnPermissionText.getText().toString().replaceAll(getString(R.string.permission_replace),"some permission text"));

如何通过替换保留下划线标签?还是我只需要以编程方式将它们添加回去?我不确定第一轮格式化有什么魔力。

任何时候你调用 toString(),你都在说 "hey, get rid of all that nice formatting that I had in there"。

The TextUtils class has a number of utility methods that can be applied directly to a CharSequence, and therefore should retain formatting. In your case, the replace() method 应该可以,至少对于简单的情况是这样。

在您的特定情况下,如果您将 This app needs PERMISSION to work properly. Update in <u>settings</u>. 更改为 This app needs %s to work properly. Update in <u>settings</u>. 并在 [=14= 中提供 %s 的值,您也可以使用 the N-parameter form of getString() ] 打电话。