gpt4 book ai didi

java - 16个按钮的随机交换位置

转载 作者:行者123 更新时间:2023-11-29 23:40:10 26 4
gpt4 key购买 nike

我必须为学校制作一个重复的游戏样本,我有 16 个按钮,每个按钮都有相同的图像。但问题是,我想在打开游戏时随机设置那 16 个按钮的位置。请任何人帮助我!

我的代码是

ImageButton bt1, bt2, bt3, bt4, bt5, bt6, bt7, bt8, bt9, bt10, bt11, bt12, bt13, bt14, bt15, bt16;

int arraypic[]={R.drawable.elephant,R.drawable.giraffe,R.drawable.hog,R.drawable.lion,R.drawable.monkey,R.drawable.panda,R.drawable.pig,R.drawable.tiger};
int button []= {R.id.bt1, R.id.bt2,R.id. bt3,R.id. bt4,R.id.bt5, R.id.bt6, R.id.bt7,R.id.bt8,R.id. bt9, R.id.bt10, R.id.bt11,R.id. bt12,R.id. bt13,R.id. bt14, R.id.bt15,R.id. bt16};
Random random = new Random();
int rr= random.nextInt(button.length);

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ingame);
bt1 = findViewById(R.id.bt1);
bt2 = findViewById(R.id.bt2);
bt3 = findViewById(R.id.bt3);
bt4 = findViewById(R.id.bt4);
bt5 = findViewById(R.id.bt5);
bt6 = findViewById(R.id.bt6);
bt7 = findViewById(R.id.bt7);
bt8 = findViewById(R.id.bt8);
bt9 = findViewById(R.id.bt9);
bt10 = findViewById(R.id.bt10);
bt11 = findViewById(R.id.bt11);
bt12 = findViewById(R.id.bt12);
bt13 = findViewById(R.id.bt13);
bt14 = findViewById(R.id.bt14);
bt15 = findViewById(R.id.bt15);
bt16 = findViewById(R.id.bt16);

bt1.setBackgroundResource(arraypic[1]);
bt2.setBackgroundResource(arraypic[1]);
bt3.setBackgroundResource(arraypic[2]);
bt4.setBackgroundResource(arraypic[2]);
bt5.setBackgroundResource(arraypic[3]);
bt6.setBackgroundResource(arraypic[3]);
bt7.setBackgroundResource(arraypic[4]);
bt8.setBackgroundResource(arraypic[4]);
bt9.setBackgroundResource(arraypic[5]);
bt10.setBackgroundResource(arraypic[5]);
bt11.setBackgroundResource(arraypic[6]);
bt12.setBackgroundResource(arraypic[6]);
bt13.setBackgroundResource(arraypic[7]);
bt14.setBackgroundResource(arraypic[7]);
bt15.setBackgroundResource(arraypic[0]);
bt16.setBackgroundResource(arraypic[0]);

最佳答案

在我看来,最简单的方法如下,使用 ListsCollections.shuffle()方法:

// collect your resources in Lists
List<Integer> pics = Arrays.asList(R.drawable.elephant, R.drawable.giraffe, R.drawable.hog, R.drawable.lion, R.drawable.monkey, R.drawable.panda, R.drawable.pig, R.drawable.tiger);
List<Integer> buttonResources = Arrays.asList(R.id.bt1, R.id.bt2, R.id.bt3, R.id.bt4, R.id.bt5, R.id.bt6, R.id.bt7, R.id.bt8, R.id.bt9, R.id.bt10, R.id.bt11, R.id.bt12, R.id.bt13,R.id.bt14, R.id.bt15, R.id.bt16);
List<ImageButton> buttons = new ArrayList<ImageButton>();

// randomize order of the pictures
Collections.shuffle(pics);

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ingame);

for(int i = 0; i < buttonResources.size(); i++)
{
ImageButton bt = findViewById(buttonResources.get(i));
buttons.add(bt);

bt.setBackgroundResource(pics.get(i/2)); // hack: integer division, works because the number of pictures is half the number of buttons
}

// access the buttons later like this:
bt1 = buttons.get(0);
}

使用Collections.shuffle(list) 方法随机化图片的顺序。这样做可以让您按顺序设置背景资源。

通过这种方式,您可以将所有按钮实例收集在一个List 中。这使您能够使用循环而不是手动列出所有按钮。如果您以后想访问单个按钮,请使用索引访问 buttons.get(index)

关于java - 16个按钮的随机交换位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51930252/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com