gpt4 book ai didi

java - 如何在 Android JAVA 中将具有 RGB 颜色的 HTML 字符串转换为具有十六进制颜色的 HTML 字符串

转载 作者:行者123 更新时间:2023-11-29 18:45:09 29 4
gpt4 key购买 nike

我有以下 html 文本(示例):

<p>​<span style="color: rgb(33, 150, 243);">This is a <span style="background-color: rgb(255, 235, 59);">test !!</span></span></p>

我正在尝试在 TextView 中显示此文本。

textview.setText(Html.fromHtml(string));

有效,但不显示任何颜色。

为此,我必须将文本转换为这种形式(使用十六进制颜色)

<p>​<span style="color: #2196f3;">This is a <span style="background-color: #ffeb3b;">test !!</span></span></p>

我该怎么做?

我找不到解决方案......有没有人遇到过这个问题并且可以帮助我?

提前致谢!

最佳答案

评论中的解释:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Stackoverflow52173062 {

public static void main(final String[] args) {

String html = "<p>​<span style=\"color: rgb(33, 150, 243);\">This is a <span style=\"background-color: rgb(255, 235, 59);\">test !!</span></span></p>";

html = replaceRGBColorsWithHex(html);

// final String
System.out.println(html);
}

private static String replaceRGBColorsWithHex(String html) {
// using regular expression to find all occurences of rgb(a,b,c) using
// capturing groups to get separate numbers.
Pattern p = Pattern.compile("(rgb\\(\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*\\))");
Matcher m = p.matcher(html);

while (m.find()) {
// get whole matched rgb(a,b,c) text
String foundRGBColor = m.group(1);
System.out.println("Found: " + foundRGBColor);

// get r value
String rString = m.group(2);
// get g value
String gString = m.group(3);
// get b value
String bString = m.group(4);

System.out.println(" separated r value: " + rString);
System.out.println(" separated g value: " + gString);
System.out.println(" separated b value: " + bString);

// converting numbers from string to int
int rInt = Integer.parseInt(rString);
int gInt = Integer.parseInt(gString);
int bInt = Integer.parseInt(bString);

// converting int to hex value
String rHex = Integer.toHexString(rInt);
String gHex = Integer.toHexString(gInt);
String bHex = Integer.toHexString(bInt);

// add leading zero if number is small to avoid converting
// rgb(1,2,3) to rgb(#123)
String rHexFormatted = String.format("%2s", rHex).replace(" ", "0");
String gHexFormatted = String.format("%2s", gHex).replace(" ", "0");
String bHexFormatted = String.format("%2s", bHex).replace(" ", "0");

System.out.println(" converted " + rString + " to hex: " + rHexFormatted);
System.out.println(" converted " + gString + " to hex: " + gHexFormatted);
System.out.println(" converted " + bString + " to hex:" + bHexFormatted);

// concatenate new color in hex
String hexColorString = "#" + rHexFormatted + gHexFormatted + bHexFormatted ;

System.out.println(" replacing " + foundRGBColor + " with " + hexColorString);
html = html.replaceAll(Pattern.quote(foundRGBColor), hexColorString);
}
return html;
}
}

关于java - 如何在 Android JAVA 中将具有 RGB 颜色的 HTML 字符串转换为具有十六进制颜色的 HTML 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52173062/

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