我不知道如何使TextView上的特定文本变成粗体。

是这样的

txtResult.setText(id+" "+name);

我希望输出是这样的:

1111年尼尔

id和名称是我从数据库中检索值的变量,我想将id改为粗体,但只有id,所以名称不会受到影响,我不知道如何做到这一点。


当前回答

首先:你不需要担心从Raghav Sood的答案使用缓慢的性能代码。

第二:使用Kotlin时,不需要编写w3bshark的答案提供的扩展函数。

最后:所有你需要做的就是从谷歌使用Kotlin android-ktx库(参考这里找到更多信息以及如何将它包含在你的项目中):

// Suppose id = 1111 and name = neil (just what you want). 
val s = SpannableStringBuilder()
          .bold { append(id) } 
          .append(name)
txtResult.setText(s) 

产量:1111尼尔


更新:

因为我认为它可以帮助其他人,也可以展示你可以走多远,这里有更多的用例。

当你需要用蓝色和斜体显示文本时: val myCustomizedString = SpannableStringBuilder() .color(blueColor, {append(“蓝色文本”)}) .append("显示") .italic{append(“这是无痛的”)} 当你需要同时以粗体和斜体显示文本时: 粗体{斜体{追加(“粗体和斜体”)}}

简而言之,粗体、追加、颜色和斜体是SpannableStringBuilder的扩展函数。您可以在官方文档中看到其他扩展函数,从那里您可以考虑其他可能性。

其他回答

字符串资源

<resources>
   <string name="your_string_resource_name">This is normal text<![CDATA[<b> but this is bold </b>]]> and <![CDATA[<u> but this is underline text</u>]]></string>
</resources> 

您的Java类

yourtextView.setText(getString(R.string.your_string_resource_name));

如果你想使用XML中的字符串,你可以这样做:

xml(“CDATA”部分很重要,否则它将不起作用)

<string name="test">
     <![CDATA[
 <b>bold!</b> normal
    ]]>
</string>

布局文件

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_gravity="center" />

</FrameLayout>

code

textView.text = HtmlCompat.fromHtml(getString(R.string.test), HtmlCompat.FROM_HTML_MODE_LEGACY)

只需要在HTML中构建字符串并设置它:

String sourceString = "<b>" + id + "</b> " + name; 
mytextview.setText(Html.fromHtml(sourceString));
val phone = "+45xxxxxx"

val phoneText = "<font color=#757B7F><b>${phone}</b></font>"

val wholeString = requireActivity().resources.getString(R.string.loginwith)+" "+phoneText

下面是我如何使用正则表达式和Kotlin来做到这一点

val BOLD_SPAN = StyleSpan(Typeface.BOLD)

    fun TextView.boldMatches(regexString: String) {
        this.applyStyleSpanToMatches(regexString, BOLD_SPAN)
    }
    

fun TextView.applyStyleSpanToMatches(regexString: String, span: StyleSpan){
        this.text = this.text.toString().applyStyleSpanToMatches(regexString, span)
        }

        fun String.applyStyleSpanToMatches(regexString: String, span: StyleSpan): Spannable {
            val result = SpannableString.valueOf(this)
            if(regexString.isEmpty()) return result
            val pattern = try{
                Pattern.compile(regexString)
            } catch (e: PatternSyntaxException){
                return result
            }
            val matcher = pattern.matcher(result)
            while (matcher.find()) {
                val start = matcher.start()
                val end = matcher.end()
                result.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            }
            return result
        }

使用问句 它可以这样应用:

txtResult.boldMatches(id)