【发布时间】: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 仍然是黑色。
【问题讨论】:
-
看看这个答案。可扩展是要走的路。 stackoverflow.com/questions/3282940/…