Android 更改文本视图中特定单词的文本背景色
我想更改Android 更改文本视图中特定单词的文本背景色,android,Android,我想更改TextView中特定单词的背景色: 如下图所示 试试这个 String str = "Some really long string goes in here"; Spannable spannable = new SpannableString(str); spannable.setSpan(new BackgroundColorSpan( getResources().getColor(android.R.color.black) ), 0, str.length(), Spa
TextView
中特定单词的背景色:
如下图所示
试试这个
String str = "Some really long string goes in here";
Spannable spannable = new SpannableString(str);
spannable.setSpan(new BackgroundColorSpan(
getResources().getColor(android.R.color.black)
), 0, str.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
yourTextView.setText(spannable);
看到这个了吗
什么是Spanable?它是如何工作的?
将指定的标记对象附加到文本的开始…结束区域,或者如果对象已附加到其他位置,则将其移动到该区域。
有关更多详细信息,请您检查并了解如何要求开始和结束编号(如中的anser 3和7)
更新
请使用此工作代码
String mainString = "As Soon As";
String subString = "Soon";
if(mainString.contains(subString)) {
int startIndex = mainString.indexOf(subString);
int endIndex = startIndex + subString.length();
SpannableString spannableString = new SpannableString(mainString);
spannableString.setSpan(new BackgroundColorSpan(Color.parseColor("#ff0000")), startIndex, endIndex,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
}
以下是我的工作代码,可用于突出显示字符串的某些部分:
private void highlightTextPart(TextView textView, int index, String regularExpression) {
String fullText = textView.getText().toString();
int startPos = 0;
int endPos = fullText.length();
String[] textParts = fullText.split(regularExpression);
if (index < 0 || index > textParts.length - 1) {
return;
}
if (textParts.length > 1) {
startPos = fullText.indexOf(textParts[index]);
endPos = fullText.indexOf(regularExpression, startPos);
if (endPos == -1) {
endPos = fullText.length();
}
}
Spannable spannable = new SpannableString(fullText);
ColorStateList blueColor = new ColorStateList(new int[][] { new int[] {}}, new int[] { Color.BLUE });
TextAppearanceSpan textAppearanceSpan = new TextAppearanceSpan(null, Typeface.BOLD_ITALIC, -1, blueColor, null);
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.GREEN);
spannable.setSpan(textAppearanceSpan, startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(backgroundColorSpan, startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannable);
}
在textview Android中更改特定单词的文本背景颜色的简单且最佳的方法
TextView txtMessage=findviewbyd(R.id.TextView\u msg);
String text=“你好,世界!”;
字符串搜索\u term=“Hello”;
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.N)
{
字符串newString=text.replaceAll(搜索词“+”搜索词+”);
txtMessage.setText(Html.fromHtml(newString
,HtmlCompat.FROM_HTML_MODE_LEGACY));
}
其他的
{
字符串newString=text.replaceAll(搜索词“+”搜索词+”);
setText(Html.fromHtml(newString));
}
我想用字符串Def=rush_searh.getDefinition()等特定单词进行更改。替换(“很快”、“很快”);tv_term.setText(Html.fromHtml(Def));@muzamilsiddiq。为此,您必须使用2或3个不同的文本视图并设置特定文本视图的背景色。我想用字符串Def=rush_searh.getDefinition等特定单词进行更改().replace(“Soon”、“Soon”);tv_term.setText(Html.fromHtml(Def));@nitimesta在代码的顶部,我认为toolBarSpan将被替换为spannableString。因为您在任何软件上都没有声明toolBarSpan。
private void highlightTextPart(TextView textView, int index, String regularExpression) {
String fullText = textView.getText().toString();
int startPos = 0;
int endPos = fullText.length();
String[] textParts = fullText.split(regularExpression);
if (index < 0 || index > textParts.length - 1) {
return;
}
if (textParts.length > 1) {
startPos = fullText.indexOf(textParts[index]);
endPos = fullText.indexOf(regularExpression, startPos);
if (endPos == -1) {
endPos = fullText.length();
}
}
Spannable spannable = new SpannableString(fullText);
ColorStateList blueColor = new ColorStateList(new int[][] { new int[] {}}, new int[] { Color.BLUE });
TextAppearanceSpan textAppearanceSpan = new TextAppearanceSpan(null, Typeface.BOLD_ITALIC, -1, blueColor, null);
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.GREEN);
spannable.setSpan(textAppearanceSpan, startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(backgroundColorSpan, startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannable);
}
int index = 3;
String regularExpression = " ";
String text = "Hello StackOverflow From BNK!";
TextView textView = (TextView) findViewById(R.id.textView);
if (textView != null) {
textView.setText(text);
highlightTextPart(textView, index, regularExpression);
}
TextView txtMessage=findViewById(R.id.textview_msg);
String text="Hello World!";
String search_term="Hello";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
{
String newString = text.replaceAll(search_term, "<span style='background:
yellow;'>"+search_term+"</span>");
txtMessage.setText(Html.fromHtml(newString
,HtmlCompat.FROM_HTML_MODE_LEGACY));
}
else
{
String newString = text.replaceAll(search_term, "<font
color='#f3f402'>"+search_term+"</font>");
txtMessage.setText(Html.fromHtml(newString));
}