在 Android 中获取 RadioGroup 中的 RadioButton 数组 [英] Get the array of RadioButtons in a RadioGroup in Android

查看:46
本文介绍了在 Android 中获取 RadioGroup 中的 RadioButton 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 Android RadioGroup 中获取 RadioButton 的数组(或集合)?我想将单个侦听器添加到单选按钮,但我没有看到任何明显的迭代它们的方式.

Is there any way of getting an array (or a collection) of the RadioButtons in an Android RadioGroup? I would like to add individual listeners to radio buttons but I don't see any obvious way of iterating over them.

推荐答案

这应该可以解决问题:

        int count = radioGroup.getChildCount();
        ArrayList<RadioButton> listOfRadioButtons = new ArrayList<RadioButton>();
        for (int i=0;i<count;i++) {
            View o = radioGroup.getChildAt(i);
            if (o instanceof RadioButton) {
                listOfRadioButtons.add((RadioButton)o);
            }
        }
        Log.d(TAG,"you have "+listOfRadioButtons.size()+" radio buttons");

这篇关于在 Android 中获取 RadioGroup 中的 RadioButton 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆