android – 突出显示通过EditText搜索的所有单词

前端之家收集整理的这篇文章主要介绍了android – 突出显示通过EditText搜索的所有单词前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
您好,我想知道如何突出显示在EditText中输入的所有单词,并将出现在TextView中此帖子与此相关 Highlight Textview Using EditText

解决方法

Say et是您的EditText,电视是TextView对象.使用以下代码
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);
                      }


        }  


            }
        });

    }

}

结果是:

原文链接:https://www.f2er.com/android/313572.html

猜你在找的Android相关文章