我正在使用此代码突出显示文本
origString = origString.replaceAll(textToHighlight,"<font color='red'>"+textToHighlight+"</font>");
Textview.setText(Html.fromHtml(origString));
此代码会在区分大小写时更改颜色,但不会更改不区分大小写的颜色!
我该如何做到这一点?
提前致谢!
更新:用户45639834回答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);
}
}
});
答案 0 :(得分:1)
要替换不区分大小写的文本,您需要使用(?i)
模式修饰符。
但是,有一点需要注意:在您的示例中,替换hello
内的aaaHELLOaaa
会导致:
aaa<font color='red'>hello</font>aaa
那是因为您搜索不区分大小写但敏感地替换。
要解决此问题,您应该使用您找到的确切结果替换文本:
replaceAll("(?i)("+textToHighlight+")", "<font color='red'>$1</font>");
作为旁注:确保从textToHighlight中删除所有括号。
答案 1 :(得分:0)
replaceAll
的第一个参数是正则表达式。要执行不区分大小写的搜索,请在"(?i)"
之前添加textToHighlight
。
答案 2 :(得分:0)
有一些简单的方法可以突出显示不区分大小写的文本。您可以在1分钟内使用其中一个示例。