如何通过循环数组设置button的id?

在Android开发中,遇到通过循环为5个Button动态分配随机ID和文本的问题。错误出现在尝试将5个Button与textList.get(5)关联。解决方法包括在XML中预先定义Button,然后在代码中利用循环设置每个Button的ID和随机文本。
摘要由CSDN通过智能技术生成

原问题来自于CSDN问答频道,更多解决方案请查看:http://ask.csdn.net/questions/1415

 

原问题描述:

通过循环设置5个button的id,每次都随机分配。

随机分配没有问题。

在使用两个方法,随机设置button的id以及设置文本的时候出现错误。

弄得很混乱不知道问题出在哪里了?

Button b1, b2, b3, b4, b5;
Button[] buttons = { b1, b2, b3, b4, b5 };

public void shuffleButtons() {

        Integer[] Id = { R.id.bChoice1, R.id.bChoice2, R.id.bChoice3,
                R.id.bChoice4, R.id.bChoice5 };

        ArrayList<Integer> buttonId = new ArrayList<Integer>(Arrays.asList(Id));

        Collections.shuffle(buttonId);


            for (int x = 0; x < 5; x++) {

                for (Button b : buttons) {

                    b = (Button) findViewById(buttonId.get(x));

                }

            }


    }

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值