7

テキストを入力するときに、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());
            }
4

3 に答える 3

8

パターンを使用して単語を検索し、任意のスパンを単語に適用できます。

@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);

    }

それでおしまい。入力した一致するすべての単語が強調表示されます。それが役立つことを願っています。

于 2014-09-16T17:27:06.383 に答える
1

Spannable の開始を から に変更しpositionましたposition - 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);
于 2013-08-24T05:44:57.940 に答える
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;
}
于 2013-08-29T05:46:25.530 に答える