我正在尝试在屏幕上随机排列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和shuffle
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。
答案 0 :(得分: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;
}