- CharSequence text = "如图所示FACE,dsfdsfdddd,如果fdsfs";
- SpannableStringBuilder builder = new SpannableStringBuilder(
- text);
- //需要替换的字符
- String rexgString = "FACE";
- Pattern pattern = Pattern.compile(rexgString);
- Matcher matcher = pattern.matcher(text);
- while (matcher.find()) {
- //找到指定的字符后 setSpan的参数分别为(指定的图片,字符的开始位置,字符的结束位置)
- builder.setSpan(new ImageSpan(MainActivity.this,
- R.drawable.face1), matcher.start(), matcher.end(),
- Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- }
- editText.setText(builder);
效果图: