问题:用户键入消息,并且在输入特定单词时,仅该单词的颜色会发生变化。这种变化是实时的。当我运行下面的代码时,应用程序继续运行并且表现得好像它不在那里。
protected void onResume() {
super.onResume();
final EditText messageBodyView = (EditText)findViewById(R.id.message_body);
String mMessageBody = messageBodyView.getText().toString();
messageBodyView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
Spannable spannable;
for (String word: mSelectedWords) {
int index = mMessageBody.indexOf(word);
spannable = new SpannableString
(mMessageBody.substring(index, index));
spannable.setSpan(new ForegroundColorSpan(Color.BLUE),0,0,0);
messageBodyView.setText(spannable);
}
}
}
});
这里发生了什么?
BUMP 我仍然无法找到解决此问题的方法,我将不胜感激任何有用的指示。
答案 0 :(得分:0)
试试这个:
edit_txt1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
@Override
public void afterTextChanged(Editable arg0) {
String[] input = edit_txt1.getText().toString().split(" ");
String output = "";
for(int i=0; i<input.length;i++){
if(input[i].equals("Word")) input[i]="<font color=red>"+input[i]+ "</font>";
}
for(int i=0; i<input.length;i++) output = output +input[i];
edit_txt1.setText(Html.fromHtml(output));
}
});