Java 仅在一行字符串中更改文本的颜色
我目前正在开发一个电子邮件应用程序,当您进入查看屏幕时,它会在顶部向您显示消息,如果是一系列电子邮件,则在下面会显示: ------原始信息------ &它下面的上一条消息 目前,所有文本均为白色,但我希望显示除以下内容之外的所有文本: ------原始消息------ 由于这是一个电子邮件应用程序,不同的提供商对原始消息字符串的格式不同(请参见下面的示例:)Java 仅在一行字符串中更改文本的颜色,java,android,string,textview,textcolor,Java,Android,String,Textview,Textcolor,我目前正在开发一个电子邮件应用程序,当您进入查看屏幕时,它会在顶部向您显示消息,如果是一系列电子邮件,则在下面会显示: ------原始信息------ &它下面的上一条消息 目前,所有文本均为白色,但我希望显示除以下内容之外的所有文本: ------原始消息------ 由于这是一个电子邮件应用程序,不同的提供商对原始消息字符串的格式不同(请参见下面的示例:) ----原始消息---- ----原始消息---- ------原始消息------ ------原始消息------ 我如何让包
- ----原始消息----
- ----原始消息----
- ------原始消息------
- ------原始消息------
String startText = "Test - received<br><br>";
String textColor = "<font color='#0475F7'>-------- Original Message --------</font><br><br>";
String endText = "From: Test 124<br>" + "To: Test 125<br>" + "Subject: " + "<" + "confirm" + ">" + "<br>" + "Sent: January 30, 2017 3:40:42 PM GMT+00:00<br><br>" + "Test";
String text = startText + textColor + endText;
messageTextView.setText(Html.fromHtml(text), TextView.BufferType.SPANNABLE);
虽然柜台会告诉我有多少破折号,我不知道如何使用它来得到整个线与破折号和“原始消息”的一部分在中间。同样,使用requiredString方法也会起作用(但在结尾处切掉一个破折号),如果用户在电子邮件中的某个地方使用了破折号,那么它就不会起作用,因为它会尝试在“原始邮件”行之前获取破折号
因此,我正在寻找一种方法来完成以下工作:
- 检索字符串中包含单词“Original Message”及其附带的所有破折号的整行
- 将颜色设置为蓝色(#0475F7)
- 将文本视图设置为消息文本在顶部(白色)+“原始消息”行(蓝色)后接消息底部(白色)
- 实现这一点的最佳方法是使用HTML/XML标记。Android拥有能够解析HTML/XML并添加所需样式的类。看这个
如果你不想走HTML路线,你也可以查看这些类
希望这能有所帮助。通过使用Spannable(如上所述)然后检查配置来解决此问题
if (message != null) {
if (message.contains("-------- Original Message --------")) {
Log.d(TAG, "Encromail configuration");
String wordToFind = "-------- Original Message --------";
Pattern word = Pattern.compile(wordToFind);
Matcher match = word.matcher(message);
Spannable wordToSpan = new SpannableString(message);
while (match.find()) {
Log.d(TAG, "Found Original Message string at index " + match.start() + " - " + (match.end() - 1));
wordToSpan.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.electric_blue)), match.start(), match.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
messageTextView.setText(wordToSpan);
} else if (message.contains("------ Original Message ------")) {
Log.d(TAG, "App to app configuration");
String wordToFind = "------ Original Message ------";
Pattern word = Pattern.compile(wordToFind);
Matcher match = word.matcher(message);
Spannable wordToSpan = new SpannableString(message);
while (match.find()) {
Log.d(TAG, "Found Original Message string at index " + match.start() + " - " + (match.end() - 1));
wordToSpan.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.electric_blue)), match.start(), match.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
messageTextView.setText(wordToSpan);
} else if (message.contains("------Original Message------")) {
Log.d(TAG, "BlackBerry 7 configuration");
String wordToFind = "------Original Message------";
Pattern word = Pattern.compile(wordToFind);
Matcher match = word.matcher(message);
Spannable wordToSpan = new SpannableString(message);
while (match.find()) {
Log.d(TAG, "Found Original Message string at index " + match.start() + " - " + (match.end() - 1));
wordToSpan.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.electric_blue)), match.start(), match.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
messageTextView.setText(wordToSpan);
} else {
Log.d(TAG, "Message doesn't contain Original Message string");
messageTextView.setText(message);
}
}
读了两遍你的问题后,我不明白你的意思,但就我所知,你为什么不使用不同的文本视图来显示破折号和消息header@MCZ简单地说,我希望更改字符串中一行文本的颜色。整个消息是一个字符串的一部分,当单击查看屏幕时,该字符串将作为意图检索:
string message=intent.getStringExtra(“消息”)代码>我可以使用两个或三个单独的文本视图,但我仍然需要从包含“-----原始消息------”的“字符串消息”中提取整行内容。因此,我会在提取特定行前后的内容以形成整个消息的同时检索整行(和所有破折号)。
if (message != null) {
if (message.contains("-------- Original Message --------")) {
Log.d(TAG, "Encromail configuration");
String wordToFind = "-------- Original Message --------";
Pattern word = Pattern.compile(wordToFind);
Matcher match = word.matcher(message);
Spannable wordToSpan = new SpannableString(message);
while (match.find()) {
Log.d(TAG, "Found Original Message string at index " + match.start() + " - " + (match.end() - 1));
wordToSpan.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.electric_blue)), match.start(), match.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
messageTextView.setText(wordToSpan);
} else if (message.contains("------ Original Message ------")) {
Log.d(TAG, "App to app configuration");
String wordToFind = "------ Original Message ------";
Pattern word = Pattern.compile(wordToFind);
Matcher match = word.matcher(message);
Spannable wordToSpan = new SpannableString(message);
while (match.find()) {
Log.d(TAG, "Found Original Message string at index " + match.start() + " - " + (match.end() - 1));
wordToSpan.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.electric_blue)), match.start(), match.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
messageTextView.setText(wordToSpan);
} else if (message.contains("------Original Message------")) {
Log.d(TAG, "BlackBerry 7 configuration");
String wordToFind = "------Original Message------";
Pattern word = Pattern.compile(wordToFind);
Matcher match = word.matcher(message);
Spannable wordToSpan = new SpannableString(message);
while (match.find()) {
Log.d(TAG, "Found Original Message string at index " + match.start() + " - " + (match.end() - 1));
wordToSpan.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.electric_blue)), match.start(), match.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
messageTextView.setText(wordToSpan);
} else {
Log.d(TAG, "Message doesn't contain Original Message string");
messageTextView.setText(message);
}
}