使用EditText突出显示Textview

时间:2012-05-28 12:05:45

标签: android textview android-edittext highlight

我目前正在制作一个像Android应用程序的搜索引擎,我想突出显示从edittext到textview的搜索词...这是我到目前为止,它只突出了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);

2 个答案:

答案 0 :(得分:5)

我想你想要突出显示用户在EditText中输入的TextView的特定单词。 说 et 是您的EditText, tv 是TextView对象。使用以下代码:


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

结果如下:

enter image description here


以下是完整的代码:

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


                }
            });

        }

    }

答案 1 :(得分:1)

它可以帮助

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