【问题标题】:Highlight Textview Using EditText使用 EditText 突出显示 Textview
【发布时间】:2012-06-02 19:19:16
【问题描述】:

我目前正在为 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);

【问题讨论】:

    标签: android textview android-edittext highlight


    【解决方案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);
    

    【讨论】:

    • 嗨,欢迎来到 SO。你应该解释你的思路来帮助用户理解你在做什么,而不是仅仅转储代码。谢谢。
    【解决方案2】:

    我认为您想突出显示用户在 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");
    

    结果如下:


    完整代码如下:

     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 个或更多相同的单词,这是否有效?
    • 非常感谢您。但是如果您知道如何突出显示在编辑文本中输入的所有相同单词,这将对我有很大帮助:D 再次感谢您
    • 对不起,我之前已经尝试过投票,但它总是说需要 15 或更多的声誉......我的声誉刚刚上升,所以现在我可以投票给你的答案......如果我的评论看起来很抱歉就像我不接受你的回答一样..
    • stackoverflow.com/questions/10799732/… 这是与这篇文章相关的单独问题...