<small id='OLvNv'></small><noframes id='OLvNv'>

    <legend id='OLvNv'><style id='OLvNv'><dir id='OLvNv'><q id='OLvNv'></q></dir></style></legend>
      <bdo id='OLvNv'></bdo><ul id='OLvNv'></ul>
    <tfoot id='OLvNv'></tfoot>

  1. <i id='OLvNv'><tr id='OLvNv'><dt id='OLvNv'><q id='OLvNv'><span id='OLvNv'><b id='OLvNv'><form id='OLvNv'><ins id='OLvNv'></ins><ul id='OLvNv'></ul><sub id='OLvNv'></sub></form><legend id='OLvNv'></legend><bdo id='OLvNv'><pre id='OLvNv'><center id='OLvNv'></center></pre></bdo></b><th id='OLvNv'></th></span></q></dt></tr></i><div id='OLvNv'><tfoot id='OLvNv'></tfoot><dl id='OLvNv'><fieldset id='OLvNv'></fieldset></dl></div>

    1. 如何在 EditText 中突出显示多个单词?

      时间:2023-10-01
        <tbody id='Qimlk'></tbody>

            <bdo id='Qimlk'></bdo><ul id='Qimlk'></ul>
            <i id='Qimlk'><tr id='Qimlk'><dt id='Qimlk'><q id='Qimlk'><span id='Qimlk'><b id='Qimlk'><form id='Qimlk'><ins id='Qimlk'></ins><ul id='Qimlk'></ul><sub id='Qimlk'></sub></form><legend id='Qimlk'></legend><bdo id='Qimlk'><pre id='Qimlk'><center id='Qimlk'></center></pre></bdo></b><th id='Qimlk'></th></span></q></dt></tr></i><div id='Qimlk'><tfoot id='Qimlk'></tfoot><dl id='Qimlk'><fieldset id='Qimlk'></fieldset></dl></div>
          • <legend id='Qimlk'><style id='Qimlk'><dir id='Qimlk'><q id='Qimlk'></q></dir></style></legend>

                <small id='Qimlk'></small><noframes id='Qimlk'>

                <tfoot id='Qimlk'></tfoot>
                本文介绍了如何在 EditText 中突出显示多个单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

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

                Currently I'm developing Spelling & Grammar checking app. It has EditText where user can input text & one button called "Check Text " upon click on button app will call LanguageTool API to check text & returns JSON response with result .

                这是应用程序的屏幕截图:

                Here is screenshot of app :

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

                Here is code which I have tried so far for highlighting multiple words but this code only highlights last word from array which I have created:

                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 等).

                I have wrote a simple method that allow you pass TextView (or child classes Button, Edittext etc.).

                1.如果您想突出显示段落中查找单词中的文本.您可以使用以下方法.

                setHighLightedText(yourTextView_Edittext_Button, "a");
                

                这会给你这样的结果.

                    /**
                     * 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 
                        }
                    });
                

                这会给你这样的结果

                /**
                 * 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 文本样式 和 另一个

                This is a workaround, you can customise spans according to your need. Some good tutorials Android text styles and one other

                这篇关于如何在 EditText 中突出显示多个单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:创建Activity时如何使EditText不聚焦 下一篇:带有芯片的 Android AutoCompleteTextView

                相关文章

                最新文章

                <i id='Bxsua'><tr id='Bxsua'><dt id='Bxsua'><q id='Bxsua'><span id='Bxsua'><b id='Bxsua'><form id='Bxsua'><ins id='Bxsua'></ins><ul id='Bxsua'></ul><sub id='Bxsua'></sub></form><legend id='Bxsua'></legend><bdo id='Bxsua'><pre id='Bxsua'><center id='Bxsua'></center></pre></bdo></b><th id='Bxsua'></th></span></q></dt></tr></i><div id='Bxsua'><tfoot id='Bxsua'></tfoot><dl id='Bxsua'><fieldset id='Bxsua'></fieldset></dl></div>

                <legend id='Bxsua'><style id='Bxsua'><dir id='Bxsua'><q id='Bxsua'></q></dir></style></legend>

                • <bdo id='Bxsua'></bdo><ul id='Bxsua'></ul>
              1. <tfoot id='Bxsua'></tfoot>
              2. <small id='Bxsua'></small><noframes id='Bxsua'>