使用 EditText 突出显示 Textview

Highlight Textview Using EditText(使用 EditText 突出显示 Textview)

本文介绍了使用 EditText 突出显示 Textview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为 android 制作一个类似应用程序的搜索引擎,我想突出显示从 edittext 到 textview 的搜索词...这是我到目前为止,它只突出显示 textview 中的第一个词

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);

推荐答案

我认为您想突出显示用户在 EditText 中键入的 TextView 的特定单词.说 et 是您的 EditText 并且 tv 是 TextView 对象.使用以下代码:

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:

完整代码如下:

 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");


                }
            });

        }

    }

这篇关于使用 EditText 突出显示 Textview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 EditText 突出显示 Textview

基础教程推荐