问题
I have searched some words in the text of a EditText. After some logic, I get the index of these words. The indices are stored in an arrayList>.
Then I used this function to highlight the color of these words.
public void changeColor(EditText et, ArrayList<ArrayList<Integer>> arr) {
Spannable wordtoSpan = new SpannableString(et.getText());
if (arr==null) return;
for (int i=0; i<arr.size(); i++){
wordtoSpan.setSpan(new BackgroundColorSpan(Color.BLUE), arr.get(i).get(0),
arr.get(i).get(1), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
et.setText(wordtoSpan, TextView.BufferType.SPANNABLE);
System.out.println("i'm changing color now");
}
}
However, only the first word was highlight even though the loop was executed by the times of number of words. I tried to replace the EditText with TextView, but still only have one word highlight. Can someone help see what's wrong here? Thank you.
回答1:
it seems to be correct, but it is better to replace your code with
public void changeColor(EditText et, ArrayList<ArrayList<Integer>> arr) {
Spannable wordtoSpan = new SpannableString(et.getText());
if (arr==null) return;
for (int i=0; i<arr.size(); i++){
wordtoSpan.setSpan(new BackgroundColorSpan(Color.BLUE), arr.get(i).get(0),
arr.get(i).get(1), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
System.out.println("i'm changing color now");
}
et.setText(wordtoSpan);
}
if after this changes, your problem remains yet, i think it might be related to your string indices in "arr"
回答2:
I am not sure but it may happen due to the behavior of edit text and textview with for or while loop. You can't achieve such behavior with for loop. I was trying to do the same for a Karaoke app, and was finally able to do this using runnable and handler. Try to use the following code.
`String text = yourEditText.getText().toString();
final String[] wordArray = text.split(" ");
final Handler handler = new Handler();
final String[] selString = new String[1];
Runnable runnable = new Runnable() {
int i=0;
@Override
public void run() {
selString[0] = wordArray[i];
i++;
String str = et.getText().toString().replaceAll(selString[0], "<font color='red'>"+ selString[0] +"</font>");
((EditText)et).setText(Html.fromHtml(str), TextView.BufferType.SPANNABLE);
if(i<wordArray.length)
handler.postDelayed(this,500);
}
};
handler.post(runnable);`
The code gets the text entered in edit text by user, split it using space, and highlight words one by one with a time gap of 500 milis.
回答3:
That is what i am using.
public void setHighLightedText(EditText editText, String textToHighlight) {
String tvt = editText.getText().toString();
int ofe = tvt.indexOf(textToHighlight, 0);
Spannable WordtoSpan = new SpannableString(editText.getText());
for (int ofs = 0; ofs < tvt.length() && ofe != -1; ofs = ofe + 1) {
ofe = tvt.indexOf(textToHighlight, ofs);
if (ofe == -1)
break;
else {
WordtoSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), ofe, ofe + textToHighlight.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.setText(WordtoSpan, TextView.BufferType.SPANNABLE);
}
}
}
来源:https://stackoverflow.com/questions/22890075/android-edittext-highlight-multiple-words-in-the-text