早些时候我得到了颜色的整数值并将其转换为十六进制以存储在数据库中,现在当我读回它并尝试将其转换回整数以使用 .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/