Java 从按钮中获取整数并将其放入数组
我的按钮里面有随机整数。我在tablelayout中创建了它们Java 从按钮中获取整数并将其放入数组,java,android,arrays,Java,Android,Arrays,我的按钮里面有随机整数。我在tablelayout中创建了它们 private static final int a=6; private static final int b=6; private int[] ids = {1,2,3,4,5,6,7,8,9}; Random rand = new Random(); private void createLayoutDynamically() { won = (TableLayout)
private static final int a=6;
private static final int b=6;
private int[] ids = {1,2,3,4,5,6,7,8,9};
Random rand = new Random();
private void createLayoutDynamically() {
won = (TableLayout)findViewById(R.id.won);
for ( int qq = 1; qq < a; qq++) {
TableRow tableRow = new TableRow(this);
for ( int q = 1; q < b; q++) {
myButton = new Button(this);
final int number = new Random().nextInt(9);
final int rand = (ids[number]);
myButton.setText(""+rand);
myButton.setTypeface(type);
myButton.setId(rand);
myButton.setTag(rand);
}
}
private static final int a=6;
专用静态最终int b=6;
私有int[]id={1,2,3,4,5,6,7,8,9};
Random rand=新的Random();
私有void createLayoutDynamics(){
won=(TableLayout)findViewById(R.id.won);
for(intqq=1;qq
我想将这些整数从我的按钮放入数组,然后如何在数组中访问这些数字?您可以在创建按钮时将这些数字添加到
int[]
int[] numberArray = new int[b];
private void createLayoutDynamically() {
...
for ( int q = 1; q < b; q++) {
...
myButton.setTag(rand);
numberArray[q] = rand;
}
}
int[]numberArray=newint[b];
私有void createLayoutDynamics(){
...
对于(int q=1;q
您需要做的是将所有对按钮的引用放在一个数组中,因此每次将它们添加到表格行
时,您都需要将它们添加到按钮数组中
获取按钮引用:
只需迭代到按钮数组并获得每个数字
示例:
创建按钮的arraylist的全局实例
private ArrayList<Button> arrayButton;
不清楚您在问什么,请更好地解释。从我生成的按钮中获取数字并将其放入数组。为什么不将随机数放入for语句中的数组中?这有什么问题?如何将它们放入数组?然后访问它们?创建一个数组,将其添加到for语句中的数组中,然后在任意位置获取,我不知道辛克,你试图用艰难的方式解决OP的问题,因为OP需要rand number没有理由保存按钮,我认为创建一个整数数组比你发布的要简单,@shayanpourvatan我想他的意思是在他将按钮添加到表格行之后。然后获取数字。好吧,那么他为什么要保存按钮数组?我想你的答案是wer是正确的,但很难(如果我正确理解OP的问题)@shayanpourvatan将按钮引用到数组中,然后从表everytimeprivate ArrayList arrayButton=new ArrayList();现在看起来像:android.widget.Button{一些文本,最后是#2}这是我想要的2个值。现在如何引用它们?例如,我想将这个数组中的两个值相乘
for ( int q = 1; q < b; q++) {
myButton = new Button(this);
final int number = new Random().nextInt(9);
final int rand = (ids[number]);
myButton.setText(""+rand);
myButton.setTypeface(type);
myButton.setId(rand);
myButton.setTag(rand);
arrayButton.add(myButton);
}
iterate to all the array of button
arrayButton.get(index).getTag