2013-08-20 3 views
6

텍스트를 입력 할 때 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()); 
      } 
+1

누구 ?? 이것은 내가 잠시 동안의 답변을 찾고 있었던 것입니다 ... – Mike6679

+0

어떤 Android 버전을 테스트하고 있습니까? 최소 및 타겟 버전? –

+0

2.2는 최소 타겟입니다. – Mike6679

답변

1

나는 position - 1position에서 Spannable의 시작을 변경 : 그래서 같은

//Color 
s.setSpan(new ForegroundColorSpan(m_color), 
      position-1, 
      position, 
      Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
//Size 
s.setSpan(new AbsoluteSizeSpan(m_curSize, true), 
      position-1, 
      position, 
      Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
+0

FYI : "Spannable 시작 위치에서 위치 - 1"로 변경하면 입력 한대로 텍스트의 색상을 지정할 수 있지만이 스타일은 매우 일관성이없고 버그가 있습니다. 나는 매우 일관되게 작동하는 강조 표시된 텍스트에 스타일을 적용하는 것으로 끝났다. – Mike6679

1

....

public static SpannableString parseActiveReply(String name, String body) { 
    SpannableString sp = new SpannableString(name + " " + body); 
    // 设置用户名字体加粗、高亮 
    sp.setSpan(new ForegroundColorSpan(Color.parseColor("#5FADC7")), 0, 
      name.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
    // sp.setSpan(new ForegroundColorSpan(Color.parseColor("#5FADC7")), 0, 
    // 0, 
    // Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
    return sp; 
} 
+0

이름 및 본문 매개 변수는 무엇을 나타 냅니까? – Mike6679

5

단어를 찾는 데 패턴을 사용할 수 있으며 단어에 임의의 범위를 적용 할 수 있습니다.

@Override 
public void afterTextChanged(Editable s) { 
    Spannable textSpan = s; 
    final Pattern pattern = Pattern.compile("\\w+"); 
    final Matcher matcher = pattern.matcher(textSpan); 
    while (matcher.find()) { 
     start = matcher.start(); 
     end = matcher.end(); 
     textSpan.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.red)), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

    } 

그게 전부입니다. 당신이 입력하는 모든 일치하는 단어를 강조 표시합니다. 도움이 되길 바랍니다.

+0

Works 100 % !!! 고마워요 !!!!!!!!!!! – Guihgo