如何更改分割字符串中的结果中的文本颜色?

时间:2015-01-18 19:35:22

标签: java android

我正在尝试创建一个总结文本的应用程序,并将该文本传递给字符串。从这里我逐字逐句地分割字符串,并搜索特定的单词。理想情况下,我希望特定的单词可以大写并涂上红色。但是,我找到的所有教程都展示了如何整体改变String的颜色。这是我的代码:

public void convertToNotes() {
    String conversionText;
    TextView  verifyTextView;



    classNotes=(EditText)findViewById(R.id.class_notes);
    className=(EditText)findViewById(R.id.class_name);
    verifyTextView = (TextView) findViewById(R.id.textView);
    verifyTextView.setSingleLine(false);
    conversionText = classNotes.getText().toString();
    String[] result = conversionText.split("//n");
    String important = "<font color=#00aeef>"+"IMPORTANT" + "</font>";
    important.toUpperCase();


    // tried to load premade textview w/ styling. Threw null pointer exception important_replace = (TextView)findViewById(R.id.important_replacement);
    //String important_detected = important_replace.toString();

    for (int a = 0; a < result.length; a++) {
        // You may want to check for a non-word character before blindly
        // performing a replacement
        // It may also be necessary to adjust the character class
        result[a] = result[a].replaceAll("sorry", "");
        result[a] = result[a].replaceAll("uh", "");
        result[a] = result[a].replaceAll("um", "");
        result[a] = result[a].replaceAll("haha", "");
        result[a] = result[a].replaceAll("nevermind", "");

         result[a] = result[a].replaceAll("important", " \n" + Html.fromHtml(important));

        verifyTextView.setText(Arrays.toString(result));
    }





}

大写字母完美无缺,但textColor仍然是黑色。

1 个答案:

答案 0 :(得分:0)

String important = "important";
SpannableString mySpannableString = new SpannableString(important .toUpperCase());//uppercase
mySpannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#f00")), 0, format.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//make it red
tv.setText(mySpannableString);//set formatted text