【问题标题】:Create and manipulate the buttons array in java and android studio在 java 和 android studio 中创建和操作按钮数组
【发布时间】: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


【解决方案1】:

正如您使用new ArrayList&lt;Integer&gt;() 创建动态答案列表一样,您也可以使用new ArrayList&lt;Button&gt;() 创建动态按钮列表。

【讨论】:

    猜你喜欢
    • 2015-09-23
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    • 2016-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    相关资源
    最近更新 更多