gpt4 book ai didi

java - 将样式应用于 Android 字符串中的文本

转载 作者:行者123 更新时间:2023-12-01 13:13:55 25 4
gpt4 key购买 nike

我正在尝试使用 SpannableString 创建一个字符串来应用颜色和粗体文本。

Spannable WordtoSpan = new SpannableString("Salida de la Capilla de María Auxiliadora\n00:00\nMarquesa de Sales\n00:00\nEspíritu Santo\n00:00\nSagasta\n00:00\nPza. Meneses\n00:00\nPozo Nuevo\n00:00\nPza. Ayuntamiento\n00:00\nSan Miguel\n00:00\nEstación de Penitencia, Iglesia San Miguel\n00:00\nÁnimas\n00:00\nMariano Hernández\n00:00\nEduardo Dato\n00:00\nRojas Marcos\n00:00\nPza. Meneses\n00:00\nCarrera\n00:00\nCalzadilla\n00:00\nGregorio María Ferro\n00:00\nMaría Auxiliadora\n00:00\nMarquesa de Sales\n00:00\nEntrada al Templo\n00:00\n");

tv.setText(WordtoSpan);

为了加快我的工作速度,我正在尝试创造这样的条件:

if(WordtoSpan.toString().contains(":")){
// found the text string ":" (00:00)

WordtoSpan.setSpan(new ForegroundColorSpan(Color.rgb(140, 117, 189)), 42, 47,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
WordtoSpan.setSpan(new StyleSpan(Typeface.BOLD), 42,47, 47);
}else{
//Not found ":"
}

我将为所有字符串 00:00 实现粗体文本和颜色。有人知道有什么快速又简单的方法吗?不去言辞地填满所有的00:00吗?

问候!

最佳答案

找到“00:00”的索引,并将其设置为span的起始索引,并将结束索引设置为“00:00”的长度

    Spannable WordtoSpan = new SpannableString("Salida de la Capilla de María Auxiliadora\n00:00\nMarquesa de Sales\n00:00\nEspíritu Santo\n00:00\nSagasta\n00:00\nPza. Meneses\n00:00\nPozo Nuevo\n00:00\nPza. Ayuntamiento\n00:00\nSan Miguel\n00:00\nEstación de Penitencia, Iglesia San Miguel\n00:00\nÁnimas\n00:00\nMariano Hernández\n00:00\nEduardo Dato\n00:00\nRojas Marcos\n00:00\nPza. Meneses\n00:00\nCarrera\n00:00\nCalzadilla\n00:00\nGregorio María Ferro\n00:00\nMaría Auxiliadora\n00:00\nMarquesa de Sales\n00:00\nEntrada al Templo\n00:00\n");

String search = new String("00:00");
int length = search.length();
String input = WordtoSpan.toString();
int startIndex = input.indexOf(search);
while(startIndex > length)
{
WordtoSpan.setSpan(new ForegroundColorSpan(Color.rgb(140, 117, 189)), startIndex, startIndex + length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
WordtoSpan.setSpan(new StyleSpan(Typeface.BOLD), startIndex, startIndex + length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
startIndex = input.indexOf(search, startIndex + length);
}

您必须循环遍历索引的字符串

关于java - 将样式应用于 Android 字符串中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22613145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com