단어가 #로 시작하면 특정 단어의 색상이 변경되어야하는 경우 편집 텍스트 안에 입력하는 동안 편집 텍스트가 있습니다.
나는 textwatcher를 구현했으며 텍스트가 #으로 시작하지만 색상을 동적으로 업데이트하는 방법을 모르는 경우,
시도했지만 SpannableStringBuilder ssb = new SpannableStringBuilder(yourText)
정적, 누구나 동적 구현을 위해 나를 도울 수 있습니까?
내 코드는 다음과 같습니다.
myEditTxt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence text, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence text, int start, int before, int count) {
if (text.charAt(start) == '#') {
//here i needs to update the typing text color
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
마침내 답을 찾았고 예상대로 작동합니다.
private int intCount = 0, initialStringLength = 0;
private String strText = "";
변경된 텍스트
@Override
public void onTextChanged(CharSequence text, int start, int before, int count) {
String strET = editText.getText().toString();
String[] str = strET.split(" ");
int cnt = 0;
if (text.length() != initialStringLength && text.length() != 0) {
if (!strET.substring(strET.length() - 1).equals(" ")) {
initialStringLength = text.length();
cnt = intCount;
for (int i = 0; i < str.length; i++)
if (str[i].charAt(0) == '#')
strText = strText + " " + "<font color='#EE0000'>" + str[i] + "</font>";
else
strText = strText + " " + str[i];
}
if (intCount == cnt) {
intCount = str.length;
editText.setText(Html.fromHtml(strText));
editText.setSelection(textShareMemories.getText().toString().length());
}
} else {
strText = "";
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다