将str1作为str2的子字符串找到正确的方法是什么,使其成为粗体(在str2中,然后是),然后将其显示为textview?
答案 0 :(得分:4)
将str2
用作SpannableString
并使用setSpan()
方法查找子字符串str1
并将其设为BOLD
。
请参阅documentation。
试试这个:
TextView textView = (TextView) findViewById(R.id.textView);
String str1 = "substring";
String str2 = "Find a substring and make it bold";
if (str2.contains(str1)) {
SpannableString spannable = new SpannableString(str2);
spannable.setSpan(new StyleSpan(android.graphics.Typeface.BOLD),
str2.indexOf(str1),
str2.indexOf(str1) + str1.length(),
Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(spannable);
} else {
textView.setText(str2);
}
<强>输出:强>
希望这会有所帮助〜
答案 1 :(得分:1)
String str1 = "hello world, how are you?";
String str2 = "world";
//check if str1 contains str2
if (!str1.contains(str2)) {
return;
}
//find index of str2
int startIndex = str1.indexOf(str2);
int endIndex = startIndex + str2.length();
//create spannable
SpannableString spannable = new SpannableString(str2;
spannable.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), startIndex, endIndex, 0);
//set it to textview
textView.setText(spannable);
答案 2 :(得分:1)
你可以使用Spannable String
if(str2.contain(str1){
SpannableStringBuilder sb = new SpannableStringBuilder(str2);
// Span to make text bold
StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD);
// make them also bold
sb.setSpan(bss, str2.indexOf(str1), str1.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(sb);
}else{
textView.setText(str2);
}
答案 3 :(得分:0)
使用indexOf查找sub1和Html.fromHtml以格式化字符串
int start = str2.indexOf(str1);
String destStr = str2.substring(start)+"<b>" + str2.substring(start, str1.length()) + "</b> " + str2.substring(start+str1.length());
textview.setText(Html.fromHtml(destStr));
答案 4 :(得分:0)
在这里评论的人的帮助下,我创建了一个也忽略大小写的函数 - 使用textView.setText(boldifySubstring(str2,str1)); -
public static Spanned boldifySubstring(String string, String substring) {
//Create spannable string
SpannableString spannable = new SpannableString(string);
//Check if substring exists (ignore case)
if (string.toLowerCase().contains(substring.toLowerCase())) {
//Get start + end indexes (ignore case)
int start = string.toLowerCase().indexOf(substring.toLowerCase());
int end = start + substring.length();
//Make relevant parts bold
spannable.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
}
return spannable;
}
答案 5 :(得分:0)
这里是一些 Kotlin 代码,它会在 CharSequence 中查找所有出现的字符串,并将指定的样式(跨度)应用于找到的出现。
"Hello World? Hello World!".applySpansToSubString("world", StyleSpan(Typeface.BOLD))
将返回“Hello World?Hello World!”
fun CharSequence.applySpansToSubString(searchString: String, vararg spans: CharacterStyle): CharSequence {
if(searchString.isEmpty()) { return this }
val sb = SpannableString(this)
var startIndex = 0
while(startIndex in 0 until length) {
startIndex = indexOf(searchString, startIndex, true)
if (startIndex >= 0) {
val endIndex = startIndex + searchString.length
for (span in spans) {
sb.setSpan(CharacterStyle.wrap(span), startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
}
startIndex = endIndex
}
}
return sb
}