简体   繁体   中英

How can I refer a string by an int in Java?

this is my configuration file

//COLORS
public final String COLORS_TOAST_BACKGROUND = "#8A2BE2";
public final String COLORS_TOAST_TEXT_COLOR = "Color.WHITE";

and this is my code

tv.setTextColor(configurationz.COLORS_TOAST_BACKGROUND);

but it doesnt work, because setTextColor takes an int

so how can I refer that color #8A2BE2 with an int, so I can use it in setTextColor()?

public static final int COLORS_TOAST_TEXT_COLOR = Color.WHITE;

See http://developer.android.com/reference/android/graphics/Color.html

The Color-Constants are already of type int.

tv.setTextColor(Color.parseColor(configurationz.COLORS_TOAST_BACKGROUND));

Store colors in colors.xml like this:

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>
<color name=”toast_background”>#8A2BE2</color>
<color name=”toast_text_color”>#8A2BE2</color>
</resources>

Then :

tv.setTextColor(getResources().getColor(R.color.toast_background);

您可以尝试使用android.graphics.Color包中提供的parseColor方法。

tv.setTextColor(Color.parseColor("#8A2BE2"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM