Java – how do I convert a string to the int value of the const class?

I have variables:

String colorName = "BLUE";

I want to set this color to paint in the Android application. It should look 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.

resolvent:

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 then set the 'colorname' variable. Things 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
分享
二维码
< <上一篇
下一篇>>