텍스트를 입력 할 때 EditText의 텍스트에 글꼴 색을 적용하려고합니다. 그러나 공간이 너무 짧으면 일관성이 없습니다. 즉, 공백을 입력하면 해당 공백 앞에 오는 텍스트가 기본 검은 색으로 돌아갑니다. 또는 커서를 단어 중간에 놓고 입력을 시작하면 입력하는 텍스트뿐 아니라 전체 단어가 색이 바뀝니다. 굵게, 기울임 꼴 및 밑줄은 잘 작동하는 것처럼 보입니다. 글꼴 색상과 관련하여 입력하는 텍스트 만 영향을 받도록 어떻게 보장 할 수 있습니까? 아래Android EditText : 입력하는 동안 전경색 범위를 적용하는 방법?
페이지의 "크기와 색상"코멘트 ...
contentEdit.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
//add style as the user types if a toggle button is enabled
ToggleButton boldButton = (ToggleButton) findViewById(R.id.bold);
ToggleButton emButton = (ToggleButton) findViewById(R.id.italic);
ToggleButton underlineButton = (ToggleButton) findViewById(R.id.underline);
int position = Selection.getSelectionStart(contentEdit.getText());
try{
if (position < 0){
position = 0;
}
if (position > 0){
if (styleStart > position || position > (cursorLoc + 1)){
//user changed cursor location, reset
if (position - cursorLoc > 1){
//user pasted text
styleStart = cursorLoc;
}
else{
styleStart = position - 1;
}
}
if (boldButton.isChecked()){
StyleSpan[] ss = s.getSpans(styleStart, position, StyleSpan.class);
for (int i = 0; i < ss.length; i++) {
if (ss[i].getStyle() == android.graphics.Typeface.BOLD){
s.removeSpan(ss[i]);
}
}
s.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), styleStart, position, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (emButton.isChecked()){
StyleSpan[] ss = s.getSpans(styleStart, position, StyleSpan.class);
for (int i = 0; i < ss.length; i++) {
if (ss[i].getStyle() == android.graphics.Typeface.ITALIC){
s.removeSpan(ss[i]);
}
}
s.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), styleStart, position, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (underlineButton.isChecked()){
UnderlineSpan[] ss = s.getSpans(styleStart, position, UnderlineSpan.class);
for (int i = 0; i < ss.length; i++) {
s.removeSpan(ss[i]);
}
s.setSpan(new UnderlineSpan(), styleStart, position, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
//SIZE AND COLOR//////////////////////////////////////////////////////
s.setSpan(new ForegroundColorSpan(m_color), position, position, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
s.setSpan(new AbsoluteSizeSpan(m_curSize, true), position, position, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
}
}
catch(Exception e){
//Toast.makeText(m_ctx, m_ctx.gets, Toast.LENGTH_LONG).show();
showMessage(R.string.NOTE_WARNING_STYLE,m_utils.MSGTYPE_WARNING);
}
cursorLoc = Selection.getSelectionStart(contentEdit.getText());
}
누구 ?? 이것은 내가 잠시 동안의 답변을 찾고 있었던 것입니다 ... – Mike6679
어떤 Android 버전을 테스트하고 있습니까? 최소 및 타겟 버전? –
2.2는 최소 타겟입니다. – Mike6679