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

是这样的

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

我希望输出是这样的:

1111年尼尔

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


当前回答

它很简单,就像这样关闭指定的文本,例如<b>"你的文本在这里:"</b>

<string name="headquarters">"<b>"Headquarters:"</b>" Mooresville, North Carolina, U.S.</string>

结果: 总部:美国北卡罗来纳州穆尔斯维尔

其他回答

val phone = "+45xxxxxx"

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

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

我创建了一个静态方法来设置文本粗体的TextView和EditText部分

public static void boldPartOfText(View mView, String contentData, int startIndex, int endIndex){
        if(!contentData.isEmpty() && contentData.length() > endIndex) {
            final SpannableStringBuilder sb = new SpannableStringBuilder(contentData);

            final StyleSpan bss = new StyleSpan(Typeface.BOLD); // Span to make text bold
            final StyleSpan iss = new StyleSpan(Typeface.NORMAL); //Span to make text normal
            sb.setSpan(iss, 0, startIndex, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
            sb.setSpan(bss, startIndex, endIndex, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make first 4 characters Bold
            sb.setSpan(iss,endIndex, contentData.length()-1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);

            if(mView instanceof TextView)
               ((TextView) mView).setText(sb);
            else if(mView instanceof EditText)
               ((EditText) mView).setText(sb);

        }
    }

另一个更定制的代码

  /*typeFaceStyle can be passed as 

    Typeface.NORMAL = 0;
    Typeface.BOLD = 1;
    Typeface.ITALIC = 2;
    Typeface.BOLD_ITALIC = 3;*/

    public static void boldPartOfText(View mView, String contentData, int startIndex, int endIndex,int typeFaceStyle){
        if(!contentData.isEmpty() && contentData.length() > endIndex) {
            final SpannableStringBuilder sb = new SpannableStringBuilder(contentData);

            final StyleSpan bss = new StyleSpan(typeFaceStyle); // Span to make text bold
            final StyleSpan iss = new StyleSpan(Typeface.NORMAL); //Span to make text italic
            sb.setSpan(iss, 0, startIndex, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
            sb.setSpan(bss, startIndex, endIndex, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make first 4 characters Bold
            sb.setSpan(iss,endIndex,contentData.length()-1,Spanned.SPAN_INCLUSIVE_INCLUSIVE);

            if(mView instanceof TextView)
                ((TextView) mView).setText(sb);
            else if(mView instanceof EditText)
                ((EditText) mView).setText(sb);
        }
    }

下面是我如何使用正则表达式和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)

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

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

字符串资源

<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));