简体   繁体   English

如何在Java中通过int引用字符串?

[英]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 但这不起作用,因为setTextColor接受一个int

so how can I refer that color #8A2BE2 with an int, so I can use it in setTextColor()? 所以我怎么能用一个int引用该颜色#8A2BE2,以便可以在setTextColor()中使用它?

public static final int COLORS_TOAST_TEXT_COLOR = Color.WHITE;

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

The Color-Constants are already of type int. 颜色常量已经是int类型的。

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

Store colors in colors.xml like this: 将颜色存储在colors.xml中,如下所示:

<?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"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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