java - 将十六进制颜色字符串转换回整数

标签 java android parsing integer hex

早些时候我得到了颜色的整数值并将其转换为十六进制以存储在数据库中,现在当我读回它并尝试将其转换回整数以使用 .setBackgroundColor(int) 我收到以下错误

java.lang.NumberFormatException: Invalid int: "ff0071dc"

在这条线上

items[i].setColourCode(Integer.parseInt(currentJourneys.get(i).getJourneyColourCode(), 16));

此外,如果我像这样硬编码十六进制值 colourLbl.setBackgroundColor(0xff0071dc); 效果很好

我做错了什么吗?我还能如何获取十六进制值并使用它来设置背景颜色?

最佳答案

我会推荐 Color.parseString() 来做。

Parse the color string, and return the corresponding color-int. If the string cannot be parsed, throws an IllegalArgumentException exception. Supported formats are: #RRGGBB #AARRGGBB or one of the following names: 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow', 'lightgray', 'darkgray', 'grey', 'lightgrey', 'darkgrey', 'aqua', 'fuchsia', 'lime', 'maroon', 'navy', 'olive', 'purple', 'silver', 'teal'.

http://developer.android.com/reference/android/graphics/Color.html#parseColor(java.lang.String)

关于java - 将十六进制颜色字符串转换回整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34574513/

相关文章:

java - <native.h> 不见了,知道它应该在哪里吗?

java - Android:你如何通过点击进入另一个 Activity ?

c - 在Arduino串口上接收固定长度的字符串并将其存储在结构中

python - 如何防止 LXML 错误 'failed to load external entity'

java - 具有长参数的 JOGL glReadPixel

java - 如何从 Action 类中的速度页面获取数据?

java - 如何使用java提取字符串正则表达式?

android - 错误 : package android. support.annotation 不存在 - Android 工作室 - Gradle

java - 您需要使用 Theme.AppCompat 主题

C 使用 strtol 解析字符串函数参数