如果用戶鍵入一個字符串,並且它包含@我想要將文本的顏色更改爲紅色.i已嘗試使用textwatcher但得到堆棧溢出錯誤。我想更改顏色只有當@位於beginning.The代碼如下在Android編輯文本中爲特定文本設置文本顏色
topic.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
if (s.toString().matches("(@\\w+)")) {
topic.setText(Html.fromHtml(s.toString().replaceAll(
"(@\\w+)", "<font color='#ffff0000'>$1</font>")));
}
}
});
您是否嘗試過同樣的邏輯之前,但與'onTextChanged',而不是'afterTextChanged' –
@SakthiKumar相同的stackoverflow錯誤 – user1767260