今天碰到RadioGroup多行多列问题,网上大多都是重写RadioGroup来实现,现在来提供另一种思路。
通过监听radiobutton的选中事件,调用RadioGroup.check( int id )方法 ,状态如果传递-1作为指定的选择标识符来清除单选按钮组的勾选状态,相当于调用clearCheck()操作
布局:
<RadioGroup
android:id="@+id/report_radioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radioBtn_goods"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="货物问题" />
<RadioButton
android:id="@+id/radioBtn_weather"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="异常天气" />
<RadioButton
android:id="@+id/radioBtn_roads"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="交通管制" />
</RadioGroup>
<RadioGroup
android:id="@+id/report_radioGroup2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radioBtn_scheduling"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="临时调度" />
<RadioButton
android:id="@+id/radioBtn_car"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="车辆故障" />
<RadioButton
android:id="@+id/radioBtn_other"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="其他事件" />
</RadioGroup>
绑定id
@BindView(R.id.report_radioGroup1)
RadioGroup report_radioGroup1;
@BindView(R.id.report_radioGroup2)
RadioGroup report_radioGroup2;
@BindView(R.id.radioBtn_goods)
RadioButton radioBtn_goods;
@BindView(R.id.radioBtn_weather)
RadioButton radioBtn_weather;
@BindView(R.id.radioBtn_roads)
RadioButton radioBtn_roads;
@BindView(R.id.radioBtn_scheduling)
RadioButton radioBtn_scheduling;
@BindView(R.id.radioBtn_car)
RadioButton radioBtn_car;
@BindView(R.id.radioBtn_other)
RadioButton radioBtn_other;
radiobutton 设置监听
@OnCheckedChanged({R.id.radioBtn_goods,R.id.radioBtn_weather,R.id.radioBtn_roads,
R.id.radioBtn_scheduling,R.id.radioBtn_car,R.id.radioBtn_other})
public void viewOnCheck(CompoundButton buttonView, boolean isChecked){
switch (buttonView.getId()){
case R.id.radioBtn_goods:
if (isChecked){
report_radioGroup2.check(-1);
ToastUtils.show(radioBtn_goods.getText().toString(),0);
}
break;
case R.id.radioBtn_weather:
if (isChecked){
report_radioGroup2.check(-1);
ToastUtils.show(radioBtn_weather.getText().toString(),0);
}
break;
case R.id.radioBtn_roads:
if (isChecked){
report_radioGroup2.check(-1);
ToastUtils.show(radioBtn_roads.getText().toString(),0);
}
break;
case R.id.radioBtn_scheduling:
if (isChecked){
report_radioGroup1.check(-1);
ToastUtils.show(radioBtn_scheduling.getText().toString(),0);
}
break;
case R.id.radioBtn_car:
if (isChecked){
report_radioGroup1.check(-1);
ToastUtils.show(radioBtn_car.getText().toString(),0);
}
break;
case R.id.radioBtn_other:
if (isChecked){
report_radioGroup1.check(-1);
ToastUtils.show(radioBtn_other.getText().toString(),0);
}
break;
}
}