【问题标题】:How Can I Change The Text Color From A Result In A Split String?如何更改拆分字符串中结果的文本颜色?
【发布时间】:2015-01-18 19:35:22
【问题描述】:

我正在尝试制作一个总结文本并将该文本传递到字符串的应用程序。从这里我逐字拆分字符串,并搜索特定的单词。理想情况下,我希望将特定单词大写并涂成红色。但是,我发现的所有教程都展示了如何整体更改字符串的颜色。这是我的代码:

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 仍然是黑色。

【问题讨论】:

标签: java android


【解决方案1】:
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

【讨论】:

    猜你喜欢
    • 2014-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多