突出显示通过 EditText 搜索的所有单词 [英] Highlight All Words that is searched via EditText

查看:19
本文介绍了突出显示通过 EditText 搜索的所有单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想知道如何突出显示在 EditText 中输入并将出现在 TextView 中的所有单词,这篇文章与这篇文章有关 使用EditText高亮Textview

Hello I want to know how to highlight all Words that is inputted in the EditText and will appear in the TextView this post is related to this one Highlight Textview Using EditText

推荐答案

et 是你的 EditText 而 tv 是 TextView 对象.使用以下代码:

Say et is your EditText and tv is TextView object. Use the following 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. Bangladesh is a land of rivers.");

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

            public void onClick(View v) {
                // TODO Auto-generated method stub
                tv.setText("The name of our country is Bangladesh. Bangladesh is a land of rivers.");
                String ett =et.getText().toString();
                String tvt =tv.getText().toString();

                int ofe = tvt.indexOf(ett,0);   
                Spannable WordtoSpan = new SpannableString( tv.getText() );

        for(int ofs=0;ofs<tvt.length() && ofe!=-1;ofs=ofe+1)
        {       


              ofe = tvt.indexOf(ett,ofs);   
                  if(ofe == -1)
                      break;
                  else
                      {                       

                      WordtoSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), ofe, ofe+ett.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                      tv.setText(WordtoSpan, TextView.BufferType.SPANNABLE);
                      }


        }  


            }
        });

    }

}

<小时>

结果是:


The result is:

这篇关于突出显示通过 EditText 搜索的所有单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆