Highlight Textview Using EditText

2019-02-07 12:35发布

问题:

Im currently making a search engine like application for android and i want to highlight the searched word from edittext to textview... this is that i got so far and it only highlights the first word in the textview

TV.setText("Hello World", TextView.BufferType.SPANNABLE);
            Spannable WordtoSpan = (Spannable) TV.getText();
            WordtoSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), 0, notes.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            TV.setText(WordtoSpan);

回答1:

I think you want to highlight a specific word of TextView which user types in a EditText. Say et is your EditText and tv is TextView object. Use the following code:


    String ett =et.getText().toString();
    String tvt =tv.getText().toString();

                int index = tvt.indexOf(ett);

                Spannable WordtoSpan = new SpannableString( tv.getText() );
                if(index != -1)
                {
                WordtoSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), index, index+ett.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                tv.setText(WordtoSpan, TextView.BufferType.SPANNABLE);
                }
                else
                tv.setText("The name of our country is Bangladesh");

Here is the outcome:


Here is the complete code:

 public class MotivationalQuotesActivity extends Activity {
        /** Called when the activity is first created. */

   Button next;
   EditText et; 
   TextView tv;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
       et = (EditText) findViewById(R.id.et);
       tv = (TextView) findViewById(R.id.tv);
       tv.setText("The name of our country is Bangladesh");

       next = (Button) findViewById(R.id.button1);
        next.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    String ett =et.getText().toString();
                    String tvt =tv.getText().toString();

                    int index = tvt.indexOf(ett);

                    Spannable WordtoSpan = new SpannableString( tv.getText() );
                    if(index != -1)
                    {
                    WordtoSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), index, index+ett.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    tv.setText(WordtoSpan, TextView.BufferType.SPANNABLE);
                    }
                    else
                    tv.setText("The name of our country is Bangladesh");


                }
            });

        }

    }


回答2:

it could help

    TextView tv = (TextView) findViewById(R.id.hello);
    SpannableString s = new SpannableString(getResources().getString(R.string.linkify));

    Pattern p = Pattern.compile("abc");


     Matcher m = p.matcher(s);

    while (m.find()) {
        int start = m.start();
        int end = m.end();
        s.setSpan(new ForegroundColorSpan(Color.RED), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    tv.setText(s);