我已经创建了一个配对游戏,它一切正常,但是你可以看到它是用一种方式制作的,我在代码中放了 4 个按钮,因为总共有 24 个按钮保持简短我想知道是否有一种随机化游戏的方法,例如可以随机化按钮名称 pic1 pic2 pic3 pic4 或 ids imageview1 2 3 4 我做了很多搜索所以不是一个急迫的问题如果我没有得到它请耐心等待并请使用我的代码告诉我所以我知道我在哪里在代码片段不适合我
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView pic1 = (ImageView ) findViewById(R.id.imageView1);
final ImageView pic2 = (ImageView ) findViewById(R.id.imageView2);
final ImageView pic3 = (ImageView ) findViewById(R.id.imageView3);
final ImageView pic4 = (ImageView ) findViewById(R.id.imageView4);
pic1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pic1.setTag("beck");
if (pic1.getTag()==("beck")) {pic1.setBackgroundResource(R.drawable.becks);}
if (pic2.getTag()==("beck")){
pic1.setVisibility(View.INVISIBLE);
pic2.setVisibility(View.INVISIBLE);}
if (pic3.getTag()==("rob") || pic4.getTag()==("rob")) {
pic1.setTag ("boot");pic1.setBackgroundResource(R.drawable.jordboots);
pic2.setTag ("boot");pic2.setBackgroundResource(R.drawable.jordboots);
pic3.setTag ("boot");pic3.setBackgroundResource(R.drawable.jordboots);
pic4.setTag ("boot");pic4.setBackgroundResource(R.drawable.jordboots); }
}});
pic2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pic2.setTag ("beck");
if (pic2.getTag()==("beck")){pic2.setBackgroundResource(R.drawable.becks);}
if (pic1.getTag()==("beck")) {
pic1.setVisibility(View.INVISIBLE);
pic2.setVisibility(View.INVISIBLE); }
if (pic3.getTag()==("rob") || pic4.getTag()==("rob")) {
pic1.setTag ("boot");pic1.setBackgroundResource(R.drawable.jordboots);
pic2.setTag ("boot");pic2.setBackgroundResource(R.drawable.jordboots);
pic3.setTag ("boot");pic3.setBackgroundResource(R.drawable.jordboots);
pic4.setTag ("boot");pic4.setBackgroundResource(R.drawable.jordboots); }
}});
pic3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pic3.setTag("rob");
if (pic3.getTag()==("rob")){pic3.setBackgroundResource(R.drawable.robs);}
if (pic4.getTag()==("rob")){
pic3.setVisibility(View.INVISIBLE);
pic4.setVisibility(View.INVISIBLE); }
if (pic1.getTag()==("beck") || pic2.getTag()==("beck")) {
pic1.setTag ("boot");pic1.setBackgroundResource(R.drawable.jordboots);
pic2.setTag ("boot");pic2.setBackgroundResource(R.drawable.jordboots);
pic3.setTag ("boot");pic3.setBackgroundResource(R.drawable.jordboots);
pic4.setTag ("boot");pic4.setBackgroundResource(R.drawable.jordboots); }
}});
pic4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pic4.setTag ("rob");
if (pic4.getTag()==("rob")){pic4.setBackgroundResource(R.drawable.robs);}
if (pic3.getTag()==("rob")){
pic3.setVisibility(View.INVISIBLE);
pic4.setVisibility(View.INVISIBLE); }
if (pic1.getTag()==("beck") || pic2.getTag()==("beck")) {
pic1.setTag ("boot");pic1.setBackgroundResource(R.drawable.jordboots);
pic2.setTag ("boot");pic2.setBackgroundResource(R.drawable.jordboots);
pic3.setTag ("boot");pic3.setBackgroundResource(R.drawable.jordboots);
pic4.setTag ("boot");pic4.setBackgroundResource(R.drawable.jordboots); }
}});
}}
这是否更接近于解决我的问题如果是这样我如何将图像分配给图像视图的背景
ArrayList<Integer> objects = new ArrayList<Integer>();
array(R.drawable.becks);
array(R.drawable.jordboots);
array(R.drawable.becks);
array(R.drawable.robs);
array(R.drawable.robs);
Collections.shuffle(objects);