[android]大小写不敏感中突出显示TextView

发布时间: 2014/8/28 20:14:44
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我使用这段代码来突出显示的文本

origString = origString.replaceAll(textToHighlight,"<font color='red'>"+textToHighlight+"</font>");

Textview.setText(Html.fromHtml(origString));

更改此代码 color 在案例-敏感,但它并不改变 color不区分大小写

我如何能实现这一点?

在此先感谢了 !

更新: 后 User45639834 的答案变得The method replaceAll(String, String) is undefined for the type new AdapterView.OnItemClickListener(){}

这是我的代码显示中的文本 dialog

    listview1.setOnItemClickListener(new OnItemClickListener(){

        public void onItemClick(AdapterView<?> parent, View view, int position, long id){

            if(Flag.querytable==0)
            {
                 final Dialog dialog;
                 Log.v("hi",((TextView) view.findViewById(R.id.textView3)).getText().toString()+"");
            if(((TextView) view.findViewById(R.id.textView3)).getText().toString()!="")
            {
            dialog = new Dialog(getActivity());
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.description);
            Typeface typeface = Typeface.createFromAsset(getActivity().getAssets(), "fonts/BOOKOS.TTF");

            TextView desc = (TextView) dialog.findViewById(R.id.textView3);//desc
            TextView part = (TextView) dialog.findViewById(R.id.textView1);
            TextView  A_name= (TextView) dialog.findViewById(R.id.textView5);
            TextView italic = (TextView) dialog.findViewById(R.id.textView2);
            TextView AS_name = (TextView) dialog.findViewById(R.id.textView4);

            Button b = (Button)dialog.findViewById(R.id.button1);

            italic.setTypeface(null, Typeface.ITALIC);

            part.setTypeface(typeface);
            A_name.setTypeface(typeface);
            italic.setTypeface(typeface);
            AS_name.setTypeface(typeface);
            desc.setTypeface(typeface);

            String origString1=((TextView) view.findViewById(R.id.textView1)).getText().toString();
            String origString2=((TextView) view.findViewById(R.id.textView2)).getText().toString();
            String origString3=((TextView) view.findViewById(R.id.textView3)).getText().toString();
            String origString4=((TextView) view.findViewById(R.id.textView4)).getText().toString();
            String origString5=((TextView) view.findViewById(R.id.textView5)).getText().toString();

            origString1 = replaceAll("(?i)("+Flag.querytext+")", "<font color='red'>$1</font>");//Error: The method replaceAll(String, String) is undefined for the type new AdapterView.OnItemClickListener(){}
            origString2 = origString2.replaceAll("(?i)"+Flag.querytext,"<font color='red'>"+Flag.querytext+"</font>");
            origString3 = origString3.replaceAll("(?i)"+Flag.querytext,"<font color='red'>"+Flag.querytext+"</font>");
            origString4 = origString4.replaceAll("(?i)"+Flag.querytext,"<font color='red'>"+Flag.querytext+"</font>");
            origString5 = origString5.replaceAll("(?i)"+Flag.querytext,"<font color='red'>"+Flag.querytext+"</font>");

            part.setText(Html.fromHtml(origString1));
            A_name.setText(Html.fromHtml(origString2));
            italic.setText(Html.fromHtml(origString3));
            AS_name.setText(Html.fromHtml(origString4));
            desc.setText(Html.fromHtml(origString5));

            Flag.s = part.getText()+"\n\n"
                    +A_name.getText()+"\n\n"
                    +italic.getText()+"\n\n"
                    +AS_name.getText()+"\n\n"
                    +desc.getText();

                    b.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {


                                    Intent ints=new Intent(getActivity(),ShareActivity.class);

                                    startActivity(ints);        }


                    });
            }
            else{
                dialog = new Dialog(getActivity());
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.description_without);
                Typeface typeface = Typeface.createFromAsset(getActivity().getAssets(), "fonts/BOOKOS.TTF");

                TextView desc = (TextView) dialog.findViewById(R.id.textView3);//desc
                TextView part = (TextView) dialog.findViewById(R.id.textView1);
                TextView  A_name= (TextView) dialog.findViewById(R.id.textView5);
                TextView AS_name = (TextView) dialog.findViewById(R.id.textView4);

                Button b = (Button)dialog.findViewById(R.id.button1);


                part.setTypeface(typeface);
                A_name.setTypeface(typeface);
                AS_name.setTypeface(typeface);
                desc.setTypeface(typeface);

               part.setText(((TextView) view.findViewById(R.id.textView1)).getText().toString());
               A_name.setText(((TextView) view.findViewById(R.id.textView2)).getText().toString());
               AS_name.setText(((TextView) view.findViewById(R.id.textView4)).getText().toString());
               desc.setText(((TextView) view.findViewById(R.id.textView5)).getText().toString());       
                Flag.s = part.getText()+"\n\n"
                        +A_name.getText()+"\n\n"
                        +AS_name.getText()+"\n\n"
                        +desc.getText();

                        b.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {


                                        Intent ints=new Intent(getActivity(),ShareActivity.class);

                                        startActivity(ints);        }


                        });                 
            }
     dialog.show();
 dialog.setCancelable(true);
 dialog.setCanceledOnTouchOutside(true);    

        }
            else if(Flag.querytable==1)
            {
                 final Dialog dialog;

            dialog = new Dialog(getActivity());
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.description_schedules);
            Typeface typeface = Typeface.createFromAsset(getActivity().getAssets(), "fonts/BOOKOS.TTF");

            TextView S_name = (TextView) dialog.findViewById(R.id.textView1);//desc
            TextView P_name = (TextView) dialog.findViewById(R.id.textView5);
            TextView  Desc= (TextView) dialog.findViewById(R.id.textView3);

            Button b = (Button)dialog.findViewById(R.id.button1);


            S_name.setTypeface(typeface);
            P_name.setTypeface(typeface);
            Desc.setTypeface(typeface);

           S_name.setText(((TextView) view.findViewById(R.id.textView2)).getText().toString());
           P_name.setText(((TextView) view.findViewById(R.id.textView3)).getText().toString());
           Desc.setText(((TextView) view.findViewById(R.id.textView4)).getText().toString());

           Flag.s = S_name.getText()+"\n\n"
                    +P_name.getText()+"\n\n"
                    +Desc.getText();

                    b.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {


                                    Intent ints=new Intent(getActivity(),ShareActivity.class);

                                    startActivity(ints);        }


                    });


                   dialog.show();
                     dialog.setCancelable(true);
                     dialog.setCanceledOnTouchOutside(true);
            }
            else{
                 final Dialog dialog;

            dialog = new Dialog(getActivity());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.description_schedules);
        Typeface typeface = Typeface.createFromAsset(getActivity().getAssets(), "fonts/BOOKOS.TTF");

        TextView S_name = (TextView) dialog.findViewById(R.id.textView1);//desc
        TextView P_name = (TextView) dialog.findViewById(R.id.textView5);
        TextView  Desc= (TextView) dialog.findViewById(R.id.textView3);

        Button b = (Button)dialog.findViewById(R.id.button1);


        S_name.setTypeface(typeface);
        P_name.setTypeface(typeface);
        Desc.setTypeface(typeface);

       S_name.setText(((TextView) view.findViewById(R.id.textView2)).getText().toString());
       P_name.setText(((TextView) view.findViewById(R.id.textView3)).getText().toString());
       Desc.setText(Html.fromHtml(((TextView) view.findViewById(R.id.textView4)).getText().toString()));

       Flag.s = S_name.getText()+"\n\n"
                +P_name.getText()+"\n\n"
                +Desc.getText();

                b.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {


                                Intent ints=new Intent(getActivity(),ShareActivity.class);

                                startActivity(ints);        }


                });


               dialog.show();
                 dialog.setCancelable(true);
                 dialog.setCanceledOnTouchOutside(true);                    
            }
        }

    });

解决方法 1:

要区分大小写替换文本您将需要使用 (?i) 模式修饰符。

然而,还有一个小的警告: 在您的示例替换 hello 里面的 aaaHELLOaaa 将导致:

aaa<font color='red'>hello</font>aaa

那是因为你搜索区分大小写,但敏感地替换。

要克服困难,应该是准确的结果,你已经找到替换文本:

replaceAll("(?i)("+textToHighlight+")", "<font color='red'>$1</font>");

作为旁注: 确保你从你的 textToHighlight 带所有括号。

赞助商