[英]How to use spannable strings in addTextChangedListener to change color of editText substring?
我正在嘗試實現類似於自動突出顯示關鍵字的代碼編輯器。 我將有一個字符串數組,當用戶鍵入文本並且它與字符串數組中的字符串匹配時,我想更改 editText 字符串的顏色和字體。 我正在使用 addTextChangeListener 但整個 editText 的文本發生了變化。 我只想突出顯示匹配的單詞。我知道我必須使用可擴展的字符串,但代碼會崩潰。 任何人都可以通過 addTextChangedListener() 幫助我正確使用可擴展字符串嗎? 這是我的代碼:
editText.addTextChangedListener(new TextWatcher() {
private SpannableString spanString;
private ForegroundColorSpan span;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
spanString = new SpannableString(s);
span = new ForegroundColorSpan(Color.YELLOW);
spanString.setSpan(span, start, start + count, 0);
}
@Override
public void afterTextChanged(Editable s) {
int beginIdx = spanString.getSpanStart(s);
int endIdx = spanString.getSpanEnd(s);
if(s.subSequence(beginIdx, endIdx).equals("for"))
{
editText.setText(spanString, EditText.BufferType.SPANNABLE);
}
spanString.removeSpan(span);
span = null;
spanString = null;
}
});
ms1996 我知道您正在嘗試創建 IDE。 要突出代碼代碼的語法,您應該使用 spannable String 或使用任何庫來使其更容易。 此外,您可以嘗試以下代碼:
I created simple code for highlight syntax in EditText. First i created a HashMap to store keywords and colors.
Map<String,Integer> map = new HashMap<>();
map.put("public",Color.CYAN);
map.put("void", Color.BLUE);
map.put("String",Color.RED);
然后我為 EditText 添加了一個 TextWatcher。 在 afterTextChanged 方法中,我使用以下代碼將 colors 設置為每個關鍵字,
........
@Override
public void afterTextChanged(Editable editable) {
String string = editable.toString();
String[] split = string.split("\\s");
for(int i = 0 ; i < split.length ; i++){
String s = split[i];
if(map.containsKey(s)){
int index = string.indexOf(s);
int color = map.get(s);
editable.setSpan(new ForegroundColorSpan(color),
index,
index + s.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
通過調用editText.setTextColor(getResources().getColor(R.color.indigo));
您正在為整個 editText 設置顏色。
您必須設置關鍵字跨度的顏色(在本例中為“for”關鍵字)。 為此,您必須知道字符串中關鍵字的 position(關鍵字的開始和結束索引)。 然后可以這樣設置。
editText.addTextChangedListener(new 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) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.toString().contains("for"))
{
Spannable wordToSpan = new SpannableString("for");
wordToSpan.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.indigo), START_INDEX_OF_THE_KEYWORD_IN_THE_STRING, END_INDEX_OF_THE_KEYWORD_IN_THE_STRING, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.setText(wordToSpan);
}
}
});
由於設置跨度需要調用 setText 還請注意,您需要防止無限循環......
試試這個,詢問任何問題。 您可以更改任何文本的顏色,只需將“for”替換為所需的文本並替換顏色 -
editText.addTextChangedListener(new 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) {
}
@Override
public void afterTextChanged(Editable s) {
String string = s.toString();
if (string.contains("for")) {
//Count to change color of all the "for"
int count = (string.length() - string.replaceAll("for", "").length())/3;
//You can make these global
int start = 0;
int beginIdx = 0;
int endIdx = 0;
for (int i = 0; i < count; i++) {
beginIdx = string.indexOf("for", start);
endIdx = beginIdx + 3;
start = endIdx;
if (beginIdx > 0)
s.setSpan(new ForegroundColorSpan(Color.GREEN),//ContextCompat.getColor(getContext(), R.color.indigo)
beginIdx, endIdx, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.