gpt4 book ai didi

java - 使用与随机播放匹配的按钮随机播放图像

转载 作者:行者123 更新时间:2023-11-30 04:29:53 25 4
gpt4 key购买 nike

我正在尝试在屏幕上随机布置 10 个编号为 1-10 的图像按钮。我使用了数组列表和集合。随机播放背景 drawables 的命令。
但我似乎无法将按钮监听器与这些随机图像联系起来。

// assigned arrays here
Button[] buttons = new Button[10];
Button[] buttonimages = new Button[10];
List<Button> list;
// Global Variables
int[] buttonarray = {R.drawable.button1,R.drawable.button2,R.drawable.button3,R.drawable.button4,R.drawable.button5,R.drawable.button6,R.drawable.button7,R.drawable.button8,R.drawable.button9,R.drawable.button10};
int idarray[] = {R.id.number1,R.id.number2,R.id.number3,R.id.number4,R.id.number5,R.id.number6,R.id.number7,R.id.number8,R.id.number9,R.id.number10};

//然后我添加到 arraylist 并随机播放

public void randomnumbers2() {

for (int z=0;z<10;z++) {
buttons[z] = (Button)findViewById(idarray[z]);
}
for (int k=0;k<10;k++) {
buttonimages[k] = (Button)findViewById(buttonarray[k]);
}
list = new ArrayList<Button>(10);
for(int i = 0; i < 10; i++) list.add(buttons[i]);
Collections.shuffle(list);

for (int z=0;z<10;z++) {
buttons[z] = (Button)findViewById(idarray[z]);
}

for (int j=0;j<10;j++){
Button b1 = (Button) findViewById(idarray[j]);
((Button) list.set(j, buttons[j])).setBackgroundResource(buttonarray[j]);
}
}

但是我的按钮是这样定义的:设置按钮

    button1 = (Button) findViewById(R.id.number1);
button2 = (Button) findViewById(R.id.number2);
button3 = (Button) findViewById(R.id.number3);
button4 = (Button) findViewById(R.id.number4);
button5 = (Button) findViewById(R.id.number5);
button6 = (Button) findViewById(R.id.number6);
button7 = (Button) findViewById(R.id.number7);
button8 = (Button) findViewById(R.id.number8);
button9 = (Button) findViewById(R.id.number9);
button10 = (Button) findViewById(R.id.number10);

问题是,当我点击第一个按钮时。它有一个数字 5 的图像,位于第一个位置,但它仍然与按钮 #1 相关联。基本上我有 2 行 5 个数字混淆。我希望按钮点击响应按钮 5,而不是按钮 1。

最佳答案

为了让你开始,

LinearLayout ll;
ArrayList<Button> buttons = new ArrayList<Button>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// create a layout
ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);

for (int i = 0; i < 10; i++) {
buttons.add(createButton(i));
}
Collections.shuffle(buttons);

for (Button b : buttons) {
ll.addView(b);
}

setContentView(ll);

}

private Button createButton(final int i) {
Button b = new Button(this);
b.setText(i + "");
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
"Clicking button: " + i, Toast.LENGTH_SHORT).show();
}

});
b.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
return b;
}

在这里,我只是尝试创建按钮并将索引设置为显示文本。您可以将背景资源设置为图片或您想要的任何内容。希望这可以帮助。


要有 2 行 5 个按钮,您需要三个线性布局。我们来获取代码...

 LinearLayout ll;
ArrayList<Button> buttons = new ArrayList<Button>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// create main layout which will host two linear layouts vertically
ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);

//create another two linear layouts which will host 5 buttons horizontally
Linear linearLayout1 = new LinearLayout(this);
Linear linearLayout2 = new LinearLayout(this);

for (int i = 0; i < 10; i++) {
buttons.add(createButton(i));
}
Collections.shuffle(buttons);

//add first 5 buttons to first layout
for (int i=0;i<5;i++) {
linearLayout1.addView(buttons.get(i));
}
//add remaining 5 to second layout
for (int i=5;i<10;i++){
linearLayout2.addView(buttons.get(i));
}
//add two layouts to main layout
ll.addView(linearLayout1);
ll.addView(linearLayout2);


//set main linear layout to be main layout of the actvitiy.
setContentView(ll);

}

private Button createButton(final int i) {
Button b = new Button(this);
b.setText(i + "");
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
"Clicking button: " + i, Toast.LENGTH_SHORT).show();
}

});
b.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
return b;
}

关于java - 使用与随机播放匹配的按钮随机播放图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7911222/

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