Создание сопоставления с образцом и сопоставление рисунка с текстом в onTextChanged Listener
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.util.Log;
import android.widget.EditText;
public class ColorEditText extends Activity {
private Pattern helloPattern = Pattern.compile("Hello");
private Pattern simplePattern = Pattern.compile("Simple");
private SpannableStringBuilder spannable;
private EditText edit;
private TextWatcher colorChangeText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color_edit_text);
edit =(EditText) findViewById(R.id.edit);
colorChangeText = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
edit.removeTextChangedListener(colorChangeText);
Log.d("Changes",edit.getText().toString());
spannable = new SpannableStringBuilder( edit.getText().toString() );
Matcher o = helloPattern.matcher(spannable);
while (o.find()) {
spannable.setSpan(new StyleSpan(Typeface.BOLD), o.start(), o.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new ForegroundColorSpan(Color.rgb(103, 58, 183)), o.start(), o.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
Matcher n = simplePattern.matcher(spannable);
while (n.find()) {
spannable.setSpan(new StyleSpan(Typeface.BOLD), n.start(), n.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new ForegroundColorSpan(Color.rgb(0, 105, 92)), n.start(), n.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
edit.setText(spannable);
edit.setSelection(spannable.length());
edit.addTextChangedListener(colorChangeText);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
};
edit.addTextChangedListener(colorChangeText);
}
}