我试图使用给定答案的这个Android: How to get a radiogroup with togglebuttons?代码的孩子为什么我不能访问无线电集团
但
static final RadioGroup.OnCheckedChangeListener ToggleListener = new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(final RadioGroup radioGroup, final int i) {
for (int j = 0; j < radioGroup.getChildCount(); j++) {
final ToggleButton view = (ToggleButton) radioGroup.getChildAt(j);
view.setChecked(view.getId() == i);
}
}
};
符合
最终切换按钮查看= (ToggleButton)radioGroup.getChildAt(j);
它总是崩溃。在Logcat中我没有看到任何消息。
我试过了我能想到的所有东西,但无法解决问题 - 非常感谢!
PS这是我的RadioGroup中的xml:
<RadioGroup android:id="@+id/radioGroup2" android:layout_width="150sp" android:layout_height="wrap_content"
android:paddingLeft = "10sp" android:layout_alignBottom="@+id/a2" >
<RadioButton android:layout_width="wrap_content" android:id="@+id/Settings_otherSettingsT2Yes" android:layout_height="wrap_content"
android:textColor="#000000" android:textSize="18sp"
android:text="@string/Settings_otherSettingsT2Yes" android:checked="false"></RadioButton>
<RadioButton android:layout_width="wrap_content" android:id="@+id/Settings_otherSettingsT2No" android:layout_height="wrap_content"
android:textColor="#000000" android:textSize="18sp"
android:text="@string/Settings_otherSettingsT2No"></RadioButton>
</RadioGroup>
......这里没有什么特别
我登录radioGroup.getChildCount()
检查的儿童人数,并让2如预期
非常感谢您的评论 - 事实上,我自己并不确定.. – user387184
顺便说一句,请解释一下这些单选按钮的setOnCheckedChangeListener和setOnClickListener的用法/区别。当我触摸屏幕时,只触发setOnCheckedChangeListener。 setOnClickListener永远不会触发 - 虽然有很多使用此示例的示例..谢谢! – user387184