Java – how do I convert a string to the int value of the const class?
•
Java
I have variables:
String colorName = "BLUE";
I want to set this color to paint in the Android application It should be like this:
paint.setColor ("Color." + colorName);
But I received an error warning because the parameter of setcolor() function should be int. what do you think? thank you.
Solution
Try this:
protected static final int colorName = Color.BLUE; paint.setColor(colorName);
Editor: as I can see, you take your color as a string So you have to check what color it is and set the 'colorname' variable Something like this:
if(yourcolorstring.equals("Blue")){ colorName = Color.BLUE; }else if(yourcolorstring.equals("Black")){ colorName = Color.BLACK; }else{ colorName = Color.WHITE; }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码