【发布时间】:2019-09-07 11:51:11
【问题描述】:
我正在设计一个简单的加法测验应用程序,其中随机生成 2 个数字,然后将它们相加。
一个正确答案和 3 个错误答案然后 setText() 到相应的按钮。
我想使用按钮数组来做同样的事情,而不是单独为按钮分配答案,因为将来可能会有大量按钮需要分配文本。
目前我正在使用 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
【问题讨论】:
-
您的问题到底是什么?并且请不要使用 android-studio 标签,除非您的问题涉及 IDE Android-Studio。
-
您好,先生,我正在将它用于 IDE android studio。这就是我使用android studio标签的原因
-
是的,但你的问题不是IDE本身,而是Android
-
哦,好的。明白了,对不起
标签: java android arrays button