如何突出显示EditText中的多个单词?

时间:2017-09-24 12:51:08

标签: java android android-edittext spannablestring

目前我正在开发Spelling&语法检查应用程序。它有EditText,用户可以在其中输入文本和文本。一个叫做" Check Text"点击按钮应用程序将调用LanguageTool API来检查文本&返回结果的JSON响应。

以下是app的屏幕截图:enter image description here

这是我迄今为止尝试突出显示多个单词的代码,但此代码仅突出显示了我创建的数组中的最后一个单词:

for (int i = 0; i < errorStrings.size(); i++) {

// Here textToCheck is EditText & errorStrings is ArrayList of type WrongString class which i have created to hold Error string , offset & length.

Spannable wordtoSpan = new SpannableString(texttoSend);
wordtoSpan.setSpan(new BackgroundColorSpan(Color.BLUE),errorStrings.get(i).getOffset(),
                                        (errorStrings.get(i).getOffset()+errorStrings.get(i).getLength()), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                            textToCheck.setText(wordtoSpan);
}

2 个答案:

答案 0 :(得分:5)

我写了一个简单的方法,允许你传递TextView(或子类ButtonEdittext等。

<强> 1。如果要突出显示段落中的单词中的文本。您可以使用以下方法,例如。

setHighLightedText(yourTextView_Edittext_Button, "a");

这会给你这样的结果。

enter image description here

    /**
     * use this method to highlight a text in TextView
     * @param tv TextView or Edittext or Button or child of TextView class
     * @param textToHighlight Text to highlight
     */
    public void setHighLightedText(TextView tv, String textToHighlight) {
        String tvt = tv.getText().toString();
        int ofe = tvt.indexOf(textToHighlight, 0);
        Spannable wordToSpan = new SpannableString(tv.getText());
        for (int ofs = 0; ofs < tvt.length() && ofe != -1; ofs = ofe + 1) {
            ofe = tvt.indexOf(textToHighlight, ofs);
            if (ofe == -1)
                break;
            else {
                wordToSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), ofe, ofe + textToHighlight.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                tv.setText(wordToSpan, TextView.BufferType.SPANNABLE);
            }
        }
    }

<强> 2。如果您想制作可点击的突出显示文字(例如点击条款和条件文字),请使用以下代码:

 setClickableHighLightedText(yourTextView_Edittext_Button, "go to settings", new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO: do your stuff here 
        }
    });

这会给你带来像

的结果

enter image description here

/**
 * use this method to set clickable highlighted a text in TextView
 *
 * @param tv              TextView or Edittext or Button or child of TextView class
 * @param textToHighlight Text to highlight
 */
public void setClickableHighLightedText(TextView tv, String textToHighlight, View.OnClickListener onClickListener) {
    String tvt = tv.getText().toString();
    int ofe = tvt.indexOf(textToHighlight, 0);
    ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            if (onClickListener != null) onClickListener.onClick(textView);
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setColor(0xff0000ff);
            ds.setUnderlineText(true);
        }
    };
    SpannableString wordToSpan = new SpannableString(tv.getText());
    for (int ofs = 0; ofs < tvt.length() && ofe != -1; ofs = ofe + 1) {
        ofe = tvt.indexOf(textToHighlight, ofs);
        if (ofe == -1)
            break;
        else {
            wordToSpan.setSpan(clickableSpan, ofe, ofe + textToHighlight.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            tv.setText(wordToSpan, TextView.BufferType.SPANNABLE);
            tv.setMovementMethod(LinkMovementMethod.getInstance());
        }
    }
}

这是一种解决方法,您可以根据需要自定义跨度。一些好的教程Android text stylesone other

答案 1 :(得分:0)

你去吧 -

  TextView myTV = (TextView)findViewById(R.id.textView1);
  String  textString = "StackOverFlow Rocks!!!"; 
  Spannable spanText = Spannable.Factory.getInstance().newSpannable(textString);
  spanText.setSpan(new BackgroundColorSpan(0xFFFFFF00), 14, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  myTV.setText(spanText);

Here is another post with similar question