How to highlight the filtered text while using SearchView widget in android

后端 未结 3 1052
误落风尘
误落风尘 2021-02-15 07:30

I have implemented SearchView Widget in my app. Its working fine. Now i need to do is, whenever i type a word in my SearchView Bar

3条回答
  •  被撕碎了的回忆
    2021-02-15 08:10

    You can use this to highlight all the keywords.

     button.setOnClickListener(new OnClickListener() {      
          @Override
          public void onClick(View v) {
    
            String ett = edittext.getText().toString();
            String tvt = textview.getText().toString();
            int ofe = tvt.indexOf(ett, 0);
            Spannable spannable = new SpannableString(tvt);
    
            for (int ofs = 0; ofs < tvt.length() && ofe != -1; ofs = ofe + 1) {
            ofe = tvt.indexOf(ett, ofs);
    
                    if (ofe == -1)
                        break;
                    else {
                      ColorStateList blueColor = new ColorStateList(new nt[][] { new int[] {} }, new int[] { Color.BLUE });
                      TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, blueColor, null);
                      spannable.setSpan(highlightSpan, ofe, ofe+edittext.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                      textview.setText(spannable);
                    }
                }
            }
        });
    

提交回复
热议问题