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

标签 java android android-edittext spannablestring

目前我正在开发拼写和语法检查应用程序。它有 EditText,用户可以在其中输入文本,点击按钮应用程序将调用 LanguageTool API 来检查文本并返回 JSON 响应和结果。

这是应用程序的屏幕截图: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);
}

最佳答案

我写了一个简单的方法,允许您传递 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());
        }
    }
}

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

关于java - 如何突出显示 EditText 中的多个单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46390221/

相关文章:

java - 用java实现LTI工具消费者

java - Logback - 根据大小滚动文件

android - 我需要用UNITY的Android和IOS应用程序设置保存图片的路径

java - 设置 ListView 元素的文本会使应用程序崩溃,测量 child 高度时出现 java 空指针异常

java - Android 唯一 key

java - 编写小型服务器的最快方法

带有自定义适配器的 android listview 卡住 Activity

android - 当将 EditText 聚焦在 ListView 或 RecyclerView 中时,键盘显示但滚动不起作用

java - maxLength 属性不适用于 EditText

java - 优先队列 poll() 时间复杂度