我正在尝试对输入字符串的指定字符进行着色。从数据库中加载输入字符串。问题是:如何给指定字符着色? :
所以,每个字符“mp”都是红色的。如果有“mp”字符如何扫描?文本是来自数据库的动态。并不总是像上面那样。我希望每个“mp”字符都是红色的。不仅在“简单”文本中,而且在每个包含“mp”字符的文本中。如果有,则将该字符着色为红色。 我正在使用 java Android 编程。
谢谢。
最佳答案
ArrayList<Integer> positions = new ArrayList();
Pattern p = Pattern.compile("mp"); // insert your pattern here
Matcher m = p.matcher("Simple Text, bumping , jumping");
while (m.find()) {
positions.add(m.start());
}
// interate this for position times
Spannable spanning = new SpannableString("Simple Text");
spanning.setSpan(new ForegroundColorSpan(Color.BLUE),positions.get(i), positions.get(i)+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textview.setText(spanning);
关于java - 在java中着色字符串的指定字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25173803/