如何初始化RadioGroup

时间:2016-09-04 07:51:54

标签: android initialization radio-group

我需要在我的应用中使用RadioGroup。但我不能在活动中初始化它。这有什么问题?

my screen for example
screen 2

1 个答案:

答案 0 :(得分:0)

你喜欢这样的事吗,

RadioGroup mRadioGroup = (RadioGroup) view.findViewById(R.id.amountcollecte_radiogroup);
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int id = mRadioGroup.getCheckedRadioButtonId();
                View radioButton = mRadioGroup.findViewById(id);
                if (radioButton.getId() == R.id.amountcollected_yes_radioButton) {


                } else if (radioButton.getId() == R.id.amountcollected_no_radioButton) {


                }
            }
        });

            <RadioButton
                android:id="@+id/amountcollected_yes_radioButton"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:buttonTint="@color/black"
                android:checked="true"
                android:gravity="center"
                android:text="Yes"
                android:textColor="@color/black"
                android:textSize="@dimen/text_large" />

            <RadioButton
                android:id="@+id/amountcollected_no_radioButton"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:buttonTint="@color/black"
                android:gravity="center"
                android:text="No"
                android:textColor="@color/black"
                android:textSize="@dimen/text_large" />
        </RadioGroup>

希望这会对你有所帮助。