在工作中遇到了话题变颜色的需求,因为写的时候遇到了一些坑,所以总结一下。
主要出现的问题是在onTextChanged()方法中,直接使用<pre name="code" class="java">myEditText.getEditableText().setSpan()用于设置满足条件的话题的颜色。
注意:绝对不能再onTextChanged()方法中使用setText()方法,不然会导致中文无法输入(当然这仅限部分手机,比如我的手机)。
package com.mxr.dreammoments.view.widget;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.widget.EditText;
import com.mxr.dreambook.R;
import com.mxr.dreambook.util.UiUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by Administrator on 2016/9/27 0027.
*/
public class MyEditText extends EditText {
private Context mContext;
private int mTopicColor = UiUtils.getResources().getColor(R.color.edit_input_username_color);//话题颜色
private int mUserNameColor = UiUtils.getResources().getColor(R.color.edit_input_username_color);//用户名称的颜色
private boolean mIsTopicBack = false;//是否从话题页返回
private int lastFlagIndex = -1;//记录最后一个有效##对中后一个#的位置
private MyTextChangeListener mMyTextChangeListener;//对文本的监听
private static final Pattern hashtagPattern =
Pattern.compile("#([^\\#|.]+)#");//满足话题的正则表达式
private MyEditText myEditText = this;//代表当前对象
public MyEditText(Context context) {
super(context);
mContext = context;
}
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
}
public MyEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
}
private class MyChangedWatcher implements TextWatcher {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
List<int[]> topicPositionList = new ArrayList<int[]>();
topicPositionList.addAll(getTopicPosition(s.toString()));
myEditText.getEditableText().setSpan(new ForegroundColorSpan(Color.BLACK), 0, s.toString().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//首先统一设置为黑色
for (int i = 0; i < topicPositionList.size(); i = i + 1) {//对满足话题部分的文字进行设置颜色
myEditText.getEditableText().setSpan(new ForegroundColorSpan(mTopicColor), topicPositionList.get(i)[0], topicPositionList.get(i)[1] + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (count > before) {//如果是增加文字
if (isJump(s.toString())) {
mMyTextChangeListener.jump();
}
}
}
@Override
public void afterTextChanged(Editable s) {
}
}
/**
* 获取需要跳转的#
* @param string
* @return
*/
private List<int[]> getTopicPosition(String string) {
char[] charArray = string.toCharArray();
int[] topicPositionArray;
List<Integer> topicList = new ArrayList<Integer>();
List<int[]> topicPositionList = new ArrayList<int[]>();
for(int i = 0; i < charArray.length; i++){
if(new Character(charArray[i]).equals('#')){
topicList.add(i);
}
}
//去除无效的#
for(int i = 0; i < topicList.size(); i++){
if ((i + 1) >= topicList.size()) {
break;
}
topicPositionArray = new int[2];
//一层过滤
if (topicList.get(i) + 1 != topicList.get(i + 1)){
topicPositionArray[0] = topicList.get(i);
topicPositionArray[1] = topicList.get(i + 1);
//二层过滤
Matcher matcher = hashtagPattern.matcher(string.substring(topicPositionArray[0], topicPositionArray[1] + 1));
if(matcher.matches()){
//三层过滤
if(topicPositionList.size() > 0){
if(topicPositionList.get(topicPositionList.size() - 1)[1] != topicPositionArray[0]){
topicPositionList.add(topicPositionArray);
}
}else{
topicPositionList.add(topicPositionArray);
}
}
}
}
if(topicPositionList.size() > 0){
lastFlagIndex = topicPositionList.get(topicPositionList.size() - 1)[1];
}else{
lastFlagIndex = -1;
}
return topicPositionList;
}
public void setMyTextChangeListener(MyTextChangeListener myTextChangeListener){
this.mMyTextChangeListener = myTextChangeListener;
addTextChangedListener(new MyChangedWatcher());
}
public interface MyTextChangeListener{
void jump();
}
/**
* 跳转逻辑
* @param string
* @return
*/
private boolean isJump(String string) {
//通过查看不需要跳转的情况
//1.#为最后一对话题的#,2.通过话题页返回的#不需要跳转.3最后一个字符为非# 4....
//对于光标不在string最后位置的,不需要跳转话题页
if(getSelectionEnd() != string.length()){
return false;
}
//最后一个字符为#,最后一对有效的##的后一个#位置不是string串最后一个#,但是最后一个#位置是其加1
if(string.endsWith("#") && lastFlagIndex != string.lastIndexOf("#") && lastFlagIndex + 1 == string.lastIndexOf("#")){
mIsTopicBack = false;
}
//最后一个字符非#
if(!string.endsWith("#")){
mIsTopicBack = false;
return false;
}
//最后一个#,并且最后一对有效的##的后一个#位置就是string串最后一个#
if(string.endsWith("#") && lastFlagIndex == string.lastIndexOf("#")){
return false;
}
//表示从话题页返回的#号对,并且满足最后一个非#
if(mIsTopicBack && !string.endsWith("#")){
return false;
}
return true;
}
public int getmTopicColor() {
return mTopicColor;
}
public void setmTopicColor(int mTopicColor) {
this.mTopicColor = mTopicColor;
}
public boolean ismIsTopicBack() {
return mIsTopicBack;
}
public void setmIsTopicBack(boolean mIsTopicBack) {
this.mIsTopicBack = mIsTopicBack;
}
public void setHintText(String hintText){
myEditText.getEditableText().setSpan(new ForegroundColorSpan(mUserNameColor), 2, hintText.lastIndexOf(":"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
public void setTopicText(String hintText){
SpannableString spannableString = new SpannableString(hintText);
spannableString.setSpan(new ForegroundColorSpan(mTopicColor), 0, hintText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
setText(spannableString);
}
public void setContentText(String contentText){
List<int[]> topicPositionList = new ArrayList<int[]>();
topicPositionList.addAll(getTopicPosition(contentText.toString()));
setText(contentText);
for (int i = 0; i < topicPositionList.size(); i = i + 1) {
myEditText.getEditableText().setSpan(new ForegroundColorSpan(mTopicColor), topicPositionList.get(i)[0], topicPositionList.get(i)[1] + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}