如何在安卓中为特定单词设置加粗样式?

huangapple 未分类评论42阅读模式
英文:

How do you set Bold style for specific word in android?

问题

我已经知道我们可以这样做,但那些不是供我使用的。我希望它只在设置的文本内,而不在 Java 中创建任何新的字符串。我在stackoverflow上已经找到了很多类似的问题,但在我的情况下它们都不适用。

以下是我尝试过的代码:

printResult.setText(String.valueOf(Html.fromHtml("<b>" + printNumbers + "</b>")));
英文:

I already know how we can do that, but those are not for my use. I want it just within set text without creating any new string in java. I found many questions already on stackoverflow, But they all are different for me in this case.

Here what i've tried :

printResult.setText(String.valueOf(Html.fromHtml(&quot;&lt;b&gt;&quot; +printNumbers+ &quot;&lt;/b&gt;&quot; )));

答案1

得分: 0

方法 String.valueOf() 会将 Spannable 转换为 String,导致您格式化的字符串丢失。因此,请移除 String.valueOf() 方法,并使用以下代码:

printResult.setText(Html.fromHtml("<b>" + printNumbers + "</b>"));
英文:

The method String.valueOf() will convert the Spannable to String and your formatted string will lost. So, remove the String.valueOf() method and use below code:

printResult.setText(Html.fromHtml(&quot;&lt;b&gt;&quot; +printNumbers+ &quot;&lt;/b&gt;&quot; ));

huangapple
  • 本文由 发表于 2020年7月24日 18:52:01
  • 转载请务必保留本文链接:https://java.coder-hub.com/63072116.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定