2013-08-20 27 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

我改变了Spannable开始从positionposition - 1

//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

仅供参考:将“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

工程100%!非常感谢 !!!!!!!!!!! – Guihgo

相关问题