在Android的一个枚举使用可绘制 [英] Android using Drawables in an Enum
问题描述
我有一个项目,我要设计钱币的数组,并使用它。我的GUI看起来是这样的 - http://i.imgur.com/eRzN3Sb.png
我希望能够从coinArray加载相应图像的每个硬币。基本上,我希望能够说的 coinView.setBackgroundResource(coinArray [X]在图像配)的我想我需要以某种方式使用可绘制对象,我希望它能够把它列入我的枚举类。枚举类看起来像
公共枚举货币{
便士(1),镍(5),迪梅(10),季(25);
私人int值;
私人货币(int值){
THIS.VALUE =价值; }}
阵列中的每个硬币都有币值这样我就可以计算它们。我想添加一个可绘制或其他一些物体,让我refernce正确的图像的每个硬币。
感谢您
公共枚举货币{
便士(1,R.drawable.xxx),镍(5,R.drawable.yyy),......;
私人int值;
私人诠释形象
私人货币(int值,诠释drawableId){
THIS.VALUE =价值;
this.image = drawableId; }
公众诠释的getImage(){
返回形象;
}}
有很多方法可以做到这一点。这是其中之一。使用它:
coinView.setImageResource(coinArray [X] .getImage());
I have a project that I have to design an array of coins and work with it. My GUI looks like this - http://i.imgur.com/eRzN3Sb.png
I want to be able to load the appropriate image from the coinArray for each coin. basically i want to be able to say coinView.setBackgroundResource(coinArray[x].image) i assume i need to somehow use a drawable object and i was hoping its possible to include it in my enum class. the enum class looks like
public enum Currency {
Penny(1), Nickel(5), Dime(10), Quarter(25);
private int value;
private Currency(int value) {
this.value = value;
}
}
Each coin in the array has a currency value so i can compute them. I'd like to add a drawable or some other object that will allow me to refernce the correct image for each coin.
Thank you
public enum Currency {
Penny(1,R.drawable.xxx), Nickel(5,R.drawable.yyy),...;
private int value;
private int image
private Currency(int value,int drawableId) {
this.value = value;
this.image=drawableId;
}
public int getImage(){
return image;
}
}
There are many ways you can do this. This is one of them. to use it:
coinView.setImageResource(coinArray[x].getImage());
这篇关于在Android的一个枚举使用可绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!