TextView中不区分大小写的突出显示

维维克·沃德(Vivek Warde)

我正在使用此代码突出显示文本

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

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

此代码以区分大小写的方式更改颜色,但不会更改不区分大小写的颜色

我怎样才能做到这一点?

提前致谢 !

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

这是我的代码,显示对话框中的文本

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

    });
西马斯

要不区分大小写地替换文本,您将需要使用(?i)pattern修饰符。

但是,有一个小警告:在您的示例中helloaaaHELLOaaa替换会导致:

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

那是因为您不区分大小写地进行搜索,但是却敏感地进行了替换。

为了克服这个问题,您应该将文本替换为找到的确切结果:

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

作为一个旁注:请确保您从[textToHighlight所有括号。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章