【问题标题】:Applying style to selected text of a String in android?将样式应用于android中字符串的选定文本?
【发布时间】:2014-04-18 12:09:26
【问题描述】:

我有一个小小的疑问,如果你能做到,我想知道。 目前,我有一个字符串,我检查是否使用“.contains("String");"比如:

TextView tv = (TextView) dialog.findViewById(R.id.txtr);

for (int x = 1; x < 2; x++) {
    tv.append("Text one\n");
    tv.append("Text Two\n");
}

if(tv.getText().toString().contains("Text one")){
    //Found
}else{
    //Not found
}

我的问题是这样的。我可以做有条件的,如果你找到我正在寻找的词,应用一个样式?

if(tv.getText().toString().contains("Text one")){
    tv.setTypeface(null, Typeface.BOLD); //I found text. Apply text bold
}else{
    //Not found
}

问候!

【问题讨论】:

  • 您只想将粗体设置为Text one
  • @Raghunandan 是的,根据情况
  • 您对 TextView 所做的更改(粗体、颜色)将影响该 TextView 中的所有字符串。听起来您正在寻找的是一个 SpannableString,它可以为字符串的某些部分设置跨度(例如粗体、颜色或字体)。

标签: java android string styles conditional


【解决方案1】:

您需要使用SpannableString

在下面,# 的所有字符都设置为红色。你可以加粗

Create clickable link in text view in android

编辑:刚刚找到了使其加粗的示例

Why doesn't my text show up with style when using SpannableStringBuilder?

【讨论】:

    猜你喜欢
    • 2014-05-02
    • 2013-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-22
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    相关资源
    最近更新 更多