我有5个Imageview,分别是Mycards_Slot1,Mycards_Slot2,Mycards_Slot3,Mycards_Slot4和Mycards_Slot5 在switch语句中,我可以使用String来设置imageview中的图像资源,而不是重新键入它们并将其用于其他imageview的问题。谢谢您。
switch(Frank_Share_counter) {
case 1:
MyCards_Slot1.setVisibility(View.VISIBLE);
if (Frank_CS1==one){MyCards_Slot1.setImageResource(R.drawable.need_one);}//need(5)
if (Frank_CS1==two){MyCards_Slot1.setImageResource(R.drawable.need_two);}
if (Frank_CS1==three){MyCards_Slot1.setImageResource(R.drawable.need_three);}
if (Frank_CS1==four){MyCards_Slot1.setImageResource(R.drawable.need_four);}
if (Frank_CS1==five){MyCards_Slot1.setImageResource(R.drawable.need_five);}
if (Frank_CS1==six){MyCards_Slot1.setImageResource(R.drawable.circle_one);}//circle(13)
if (Frank_CS1==seven){MyCards_Slot1.setImageResource(R.drawable.circle_two);}
if (Frank_CS1==eight){MyCards_Slot1.setImageResource(R.drawable.circle_three);}
if (Frank_CS1==nine){MyCards_Slot1.setImageResource(R.drawable.circle_four);}
if (Frank_CS1==ten){MyCards_Slot1.setImageResource(R.drawable.circle_five);}
if (Frank_CS1==eleven){MyCards_Slot1.setImageResource(R.drawable.circle_seven);}
if (Frank_CS1==twelve){MyCards_Slot1.setImageResource(R.drawable.circle_eight);}
if (Frank_CS1==thirteen){MyCards_Slot1.setImageResource(R.drawable.circle_nine);}
if (Frank_CS1==fourteen){MyCards_Slot1.setImageResource(R.drawable.circle_ten);}
if (Frank_CS1==fifteen){MyCards_Slot1.setImageResource(R.drawable.circle_eleven);}
if (Frank_CS1==sixteen){MyCards_Slot1.setImageResource(R.drawable.circle_twelve);}
if (Frank_CS1==seventeen){MyCards_Slot1.setImageResource(R.drawable.circle_thieteen);}
if (Frank_CS1==eighteen){MyCards_Slot1.setImageResource(R.drawable.cross_one);}//cross(9)
if (Frank_CS1==nineteen){MyCards_Slot1.setImageResource(R.drawable.cross_two);}
if (Frank_CS1==twenty){MyCards_Slot1.setImageResource(R.drawable.cross_three);}
if (Frank_CS1==twenty_one){MyCards_Slot1.setImageResource(R.drawable.cross_five);}
if (Frank_CS1==twenty_two){MyCards_Slot1.setImageResource(R.drawable.cross_seven);}
if (Frank_CS1==twenty_three){MyCards_Slot1.setImageResource(R.drawable.cross_ten);}
if (Frank_CS1==twenty_four){MyCards_Slot1.setImageResource(R.drawable.cross_eleven);}
if (Frank_CS1==twenty_five){MyCards_Slot1.setImageResource(R.drawable.cross_thirteen);}
if (Frank_CS1==twenty_six){MyCards_Slot1.setImageResource(R.drawable.cross_fourteen);}
if (Frank_CS1==twenty_seven){MyCards_Slot1.setImageResource(R.drawable.square_one);}//square(11)
if (Frank_CS1==twenty_eight){MyCards_Slot1.setImageResource(R.drawable.square_two);}
if (Frank_CS1==twenty_nine){MyCards_Slot1.setImageResource(R.drawable.square_three);}
if (Frank_CS1==thirty){MyCards_Slot1.setImageResource(R.drawable.square_five);}
if (Frank_CS1==thirty_one){MyCards_Slot1.setImageResource(R.drawable.square_seven);}
if (Frank_CS1==thirty_two){MyCards_Slot1.setImageResource(R.drawable.square_eight);}
if (Frank_CS1==thirty_three){MyCards_Slot1.setImageResource(R.drawable.square_ten);}
if (Frank_CS1==thirty_four){MyCards_Slot1.setImageResource(R.drawable.square_eleven);}
if (Frank_CS1==thirty_five){MyCards_Slot1.setImageResource(R.drawable.square_twelve);}
if (Frank_CS1==thirty_six){MyCards_Slot1.setImageResource(R.drawable.square_thirteen);}
if (Frank_CS1==thirty_seven){MyCards_Slot1.setImageResource(R.drawable.star_one);}//star(7)
if (Frank_CS1==thirty_eight){MyCards_Slot1.setImageResource(R.drawable.star_two);}
if (Frank_CS1==thirty_nine){MyCards_Slot1.setImageResource(R.drawable.star_three);}
if (Frank_CS1==forty){MyCards_Slot1.setImageResource(R.drawable.star_four);}
if (Frank_CS1==forty_one){MyCards_Slot1.setImageResource(R.drawable.star_five);}
if (Frank_CS1==forty_two){MyCards_Slot1.setImageResource(R.drawable.star_seven);}
if (Frank_CS1==forty_three){MyCards_Slot1.setImageResource(R.drawable.star_eight);}
if (Frank_CS1==forty_four){MyCards_Slot1.setImageResource(R.drawable.triangle_one);}//Triangle(12)
if (Frank_CS1==forty_five){MyCards_Slot1.setImageResource(R.drawable.triangle_two);}
if (Frank_CS1==forty_six){MyCards_Slot1.setImageResource(R.drawable.triangle_three);}
if (Frank_CS1==forty_seven){MyCards_Slot1.setImageResource(R.drawable.triangle_four);}
if (Frank_CS1==forty_eight){MyCards_Slot1.setImageResource(R.drawable.triangle_five);}
if (Frank_CS1==forty_nine){MyCards_Slot1.setImageResource(R.drawable.triangle_seven);}
if (Frank_CS1==fifty){MyCards_Slot1.setImageResource(R.drawable.triangle_eight);}
if (Frank_CS1==fifty_one){MyCards_Slot1.setImageResource(R.drawable.triangle_ten);}
if (Frank_CS1==fifty_two){MyCards_Slot1.setImageResource(R.drawable.triangle_eleven);}
if (Frank_CS1==fifty_three){MyCards_Slot1.setImageResource(R.drawable.triangle_twelve);}
if (Frank_CS1==fifty_four){MyCards_Slot1.setImageResource(R.drawable.triangle_thirteen);}
if (Frank_CS1==fifty_five){MyCards_Slot1.setImageResource(R.drawable.triangle_fourteen);}
if (Frank_CS1==fifty_six){MyCards_Slot1.setImageResource(R.drawable.circle_fourteen);}//by mistake
if (Frank_CS1==fifty_seven){MyCards_Slot1.setImageResource(R.drawable.square_fourteen);}//by mistake
break;
default:
Toast.makeText(this, "Could not find song", Toast.LENGTH_LONG).show();
}
答案 0 :(得分:0)
我正在根据我对您问题的理解方式进行回答:
我假设您想以某种方式引用drawables
,那么您可以在int
中设置资源ID
//reference them like this:
int need_one = R.drawable.need_one;
int need_two = R.drawable.need_one;
int need_three = R.drawable.need_three;
//.......
//complete the rest by yourself
//and just use them like this:
.........
if (Frank_CS1==one){MyCards_Slot1.setImageResource(need_one);}
if (Frank_CS1==two){MyCards_Slot1.setImageResource(need_two);}
if (Frank_CS1==three){MyCards_Slot1.setImageResource(need_three);}
.....
....
....