Android通过字符串循环到特定颜色的单词
我有一个字符串,其中有我想要着色的特定单词。这些词是以#开头的Android通过字符串循环到特定颜色的单词,android,string,Android,String,我有一个字符串,其中有我想要着色的特定单词。这些词是以#开头的 你需要循环你的比赛。您可以在此处查看以前的帖子: 使用while(),要设置在android native messenger中搜索的文本的背景色,您可以在代码中尝试相同的设置。检查以下示例: String title = "Your #complete sentence #testing #test."; printUsingNormal(title); printUsingRegex(title); private stati
你需要循环你的比赛。您可以在此处查看以前的帖子:
使用
while()
,要设置在android native messenger中搜索的文本的背景色,您可以在代码中尝试相同的设置。检查以下示例:
String title = "Your #complete sentence #testing #test.";
printUsingNormal(title);
printUsingRegex(title);
private static void printUsingRegex(String title) {
Pattern pattern = Pattern.compile("[ ,\\.\\n]");
int start = 0;
int end = 0;
Matcher matcher = pattern.matcher(title);
while (end >= 0 && end < title.length()) {
start = title.indexOf('#', start);
if (start > 0) {
end = matcher.find(start) ? matcher.start() : -1;
if (end > 0) {
System.out.println("Word : " + title.substring(start, end)
+ " Start : " + start + " End : " + end);
start = end;
}
} else {
break;
}
}
}
private static void printUsingNormal(String title) {
int start = 0;
for (int i = 0; i < title.length(); i++) {
char c = title.charAt(i);
if (c == '#') {
start = i; // Got the starting hash
int end = title.indexOf(' ', i + 1); // Finding the next space.
if (end > 0) {
System.out.println("Word : " + title.substring(start, end)
+ " Start : " + start + " End : " + end);
i = end;
} else {
end=title.length()-1;
System.out.println("Word : " + title.substring(start, end)
+ " Start : " + start + " End : " + end);
i = end;
}
}
}
}
String title=“您的#完整句子#测试#测试。”;
使用普通打印(标题);
打印使用正则表达式(标题);
私有静态void打印使用regex(字符串标题){
Pattern=Pattern.compile(“[,\\.\\n]”);
int start=0;
int end=0;
Matcher Matcher=pattern.Matcher(标题);
而(end>=0&&end0){
end=matcher.find(start)?matcher.start():-1;
如果(结束>0){
System.out.println(“Word:”+title.substring(开始,结束)
+“开始:”+开始+“结束:”+结束);
开始=结束;
}
}否则{
打破
}
}
}
私有静态无效打印使用普通(字符串标题){
int start=0;
对于(int i=0;i0){
System.out.println(“Word:”+title.substring(开始,结束)
+“开始:”+开始+“结束:”+结束);
i=结束;
}否则{
end=title.length()-1;
System.out.println(“Word:”+title.substring(开始,结束)
+“开始:”+开始+“结束:”+结束);
i=结束;
}
}
}
}
我很惊讶没有人提到这一点:
// String you want to perform on
String toChange = "I love cheese #burgers, and with #ketchup.";
// Matches all characters, numbers, underscores and dashes followed by '#'
// Does not match '#' followed by space or any other non word characters
toChange = toChange.replaceAll("(#[A-Za-z0-9_-]+)",
"<font color='#0000ff'>" + "$0" + "</font>");
// Encloses the matched characters with html font tags
// Html#fromHtml(String) returns a Spanned object
holder.txtTitle.setText(Html.fromHtml(toChange));
//要对其执行的字符串
String toChange=“我喜欢奶酪、汉堡和番茄酱。”;
//匹配所有字符、数字、下划线和破折号,后跟“#”
//与后跟空格或任何其他非单词字符的“#”不匹配
toChange=toChange.replaceAll((#[A-Za-z0-9_-]+)”,
"" + "$0" + "");
//用html字体标记封闭匹配的字符
//Html#fromHtml(字符串)返回一个跨越的对象
holder.txtitle.setText(Html.fromHtml(toChange));
#0000ff
==>颜色.蓝色
截图:
我刚才在这里提供了这个答案- [编辑] 我已经修改了代码,并根据您的需要进行了修改:
SpannableString hashText = new SpannableString(text.getText().toString());
Matcher matcher = Pattern.compile("#([A-Za-z0-9_-]+)").matcher(hashText);
while (matcher.find())
{
hashText.setSpan(new ForegroundColorSpan(Color.BLUE), matcher.start(), matcher.end(), 0);
}
text.setText(hashText);
以下是它的屏幕截图:
找到它们很好(我可以使用.split(“#”),但是如何在字符串中同时找到它们并给它们上色呢?if(title.contains(“#”){Matcher Matcher=pattern.Matcher(title);while(Matcher.find()){//do ForegroundcolorSpan etc}检查我的更新问题,我尝试使用while。现在列表上没有显示任何内容。请查找我上面的更新答案。它同时使用正则表达式和直接方式。嗯,现在我很困惑。我现在如何使用它?你能用旧答案使用正则表达式吗?(所以正则表达式+循环int I)请在我的答案更新中查看我的最新尝试。看看我的意思,我遗漏了什么。@JohnJared在上面添加了一个屏幕截图。问题可能在你的代码中的其他地方吗?嗯,找出原因。因为在我的应用程序中我有一个带有#这就是为什么,但当我将阿拉伯语改为英语时,它们起作用了。我如何使它也为阿拉伯语单词着色?أبتث我应该等16个小时,16个小时后我会给你+50,谢谢!不,但我错给了那个人+50。对不起。我会就下一个问题开始另一笔奖金,我会奖励你的。我不知道怎么做“对不起”,我是一名初学者,正在尝试与我的兄弟一起开发。因此,如何提供结束、开始等…@JohnJared请使用您想要的代码示例检查我的更新答案
// String you want to perform on
String toChange = "I love cheese #burgers, and with #ketchup.";
// Matches all characters, numbers, underscores and dashes followed by '#'
// Does not match '#' followed by space or any other non word characters
toChange = toChange.replaceAll("(#[A-Za-z0-9_-]+)",
"<font color='#0000ff'>" + "$0" + "</font>");
// Encloses the matched characters with html font tags
// Html#fromHtml(String) returns a Spanned object
holder.txtTitle.setText(Html.fromHtml(toChange));
String text = "I love chicken #burger , cuz they are #delicious";
//get the text from TextView
Pattern HASHTAG_PATTERN = Pattern.compile("#(\\w+|\\W+)");
Matcher mat = HASHTAG_PATTERN.matcher(text);
while (mat.find()) {
String tag = mat.group(0);
//String tag will contain the hashtag
//do operations with the hashtag (change color)
}
SpannableString hashText = new SpannableString(text.getText().toString());
Matcher matcher = Pattern.compile("#([A-Za-z0-9_-]+)").matcher(hashText);
while (matcher.find())
{
hashText.setSpan(new ForegroundColorSpan(Color.BLUE), matcher.start(), matcher.end(), 0);
}
text.setText(hashText);