Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java和android studio中创建和操作按钮数组_Java_Android_Arrays_Button - Fatal编程技术网

在java和android studio中创建和操作按钮数组

在java和android studio中创建和操作按钮数组,java,android,arrays,button,Java,Android,Arrays,Button,我正在设计一个简单的加法测验应用程序,其中随机生成两个数字,然后将它们相加 然后将一个正确答案和三个错误答案分别设置到相应的按钮上 我想使用按钮数组来做同样的事情,而不是单独为按钮分配答案,因为将来可能会有大量按钮需要分配文本 目前我正在使用ArrayList将其元素逐个分配给按钮。 我没有面临任何问题,但这是可以的,因为只有4个按钮。对于更多的按钮,我更喜欢数组来分配文本 //Part 1: Declaration Button optionA,optionB,optionC,optionD;

我正在设计一个简单的加法测验应用程序,其中随机生成两个数字,然后将它们相加

然后将一个正确答案和三个错误答案分别设置到相应的按钮上

我想使用按钮数组来做同样的事情,而不是单独为按钮分配答案,因为将来可能会有大量按钮需要分配文本

目前我正在使用ArrayList将其元素逐个分配给按钮。 我没有面临任何问题,但这是可以的,因为只有4个按钮。对于更多的按钮,我更喜欢数组来分配文本

//Part 1: Declaration
Button optionA,optionB,optionC,optionD;
ArrayList<Integer> answers = new ArrayList<Integer>();

//Part 2: Allot address
optionA = (Button) findViewbyid(R.id.optionA); // button variable name and address are same
optionB = (Button) findViewbyid(R.id.optionB);
optionC = (Button) findViewbyid(R.id.optionC);
optionD = (Button) findViewbyid(R.id.optionD);

//Part 3: Send Arraylist values to button:

        optionA.setText(Integer.toString(answers.get(0)));
        optionB.setText(Integer.toString(answers.get(1)));
        optionC.setText(Integer.toString(answers.get(2)));
        optionD.setText(Integer.toString(answers.get(3)));

//Please note that entire code is not given here. Value is assigned to ArrayList elements using answers.add(i,value) in for loop
//第1部分:声明
按钮选项A、选项B、选项C、选项D;
ArrayList answers=新的ArrayList();
//第2部分:分配地址
optionA=(按钮)findViewbyid(R.id.optionA);//按钮变量名称和地址相同
optionB=(按钮)findViewbyid(R.id.optionB);
optionC=(按钮)findViewbyid(R.id.optionC);
optionD=(按钮)findViewbyid(R.id.optionD);
//第3部分:将Arraylist值发送到按钮:
optionA.setText(Integer.toString(answers.get(0));
optionB.setText(Integer.toString(answers.get(1));
optionC.setText(Integer.toString(answers.get(2));
optionD.setText(Integer.toString(answers.get(3));
//请注意,这里没有给出完整的代码。使用答案将值分配给ArrayList元素。在for循环中添加(i,值)

正如您使用
新建ArrayList()
创建动态答案列表一样,您也可以使用
新建ArrayList()
创建动态按钮列表。

您到底有什么问题?请不要使用android studio标签,除非您的问题涉及IDE android-studio。您好,先生,我正在为IDE android studio使用它。这就是为什么我使用android studio tagYeah,但你的问题不是指IDE本身,而是指AndroidOh ok。明白了,抱歉