导语
如下图所示,这是一个导航选择弹框。进行单项选择,然后会监听回调选择的事件。问题是Android的RadioButton是一般是放在RadioGroup中进行管理的,而RadioGroup又是线性布局,即单行有效,多行无效。如图那种就需要两个RadioGroup来进行合作。那么如果进行合作呢?
分析:RadioGroup多行显示其实就是两个RadioGroup进行切换,使用RadioGroup的clearCheck()方法进行操作。
但是在使用clearCheck()之前,如果RadioGroup已经设置过监听,执行就会报StackOveflowError错误。
正确方法如下:
先调用
setOnCheckedChangeListener(null)
再调用
clearCheck()然后重新setOnCheckedChangeListener(checkedListener)。
案例代码
1、布局如下:
<RadioGroup
android:id="@+id/rg_manhole_state_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/tv_manhole_state"
android:orientation="horizontal"
android:paddingTop="@dimen/padding_5"
<RadioButton
android:id="@+id/rb_intact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:drawableLeft="@drawable/bg_radiobutten"
android:drawablePadding="@dimen/padding_10"
android:text="@string/intact"
android:textColor="@color/white"
android:textSize="@dimen/small_size" /
<RadioButton
android:id="@+id/rb_lose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:button="@null"
android:drawableLeft="@drawable/bg_radiobutten"
android:drawablePadding="@dimen/padding_10"
android:text="@string/lose"
android:textColor="@color/white"
android:textSize="@dimen/small_size" /
<RadioButton
android:id="@+id/rb_sunken"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:button="@null"
android:drawableLeft="@drawable/bg_radiobutten"
android:drawablePadding="@dimen/padding_10"
android:text="@string/sunken"
android:textColor="@color/white"
android:textSize="@dimen/small_size" /
</RadioGroup
<RadioGroup
android:id="@+id/rg_manhole_state_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/padding_100"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:paddingTop="@dimen/padding_5"
<RadioButton
android:id="@+id/rb_occupation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:drawableLeft="@drawable/bg_radiobutten"
android:drawablePadding="@dimen/padding_10"
android:text="@string/occupation"
android:textColor="@color/white"
android:textSize="@dimen/small_size" /
<RadioButton
android:id="@+id/rb_damage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:button="@null"
android:drawableLeft="@drawable/bg_radiobutten"
android:drawablePadding="@dimen/padding_10"
android:text="@string/damage"
android:textColor="@color/white"
android:textSize="@dimen/small_size" /
<RadioButton
android:id="@+id/rb_heave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:button="@null"
android:drawableLeft="@drawable/bg_radiobutten"
android:drawablePadding="@dimen/padding_10"
android:text="@string/heave"
android:textColor="@color/white"
android:textSize="@dimen/small_size" /
</RadioGroup
2、具体操作如下:
声明控件:
@InjectView(R.id.rg_manhole_state_one)
RadioGroup rgManholeStateOne;
@InjectView(R.id.rg_manhole_state_two)
RadioGroup rgManholeStateTwo;
设置监听:
rgManholeStateOne.setOnCheckedChangeListener(new OnMyManholeStateOneCheckedChangeListener());
rgManholeStateTwo.setOnCheckedChangeListener(new OnMyManholeStateTwoCheckedChangeListener());
实现单选:
private class OnMyManholeStateOneCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int position) {
switch (position) {
case R.id.rb_intact:
if (rbIntact.isChecked())
rgManholeStateTwo.clearCheck();
break;
case R.id.rb_lose:
if (rbLose.isChecked())
rgManholeStateTwo.clearCheck();
break;
case R.id.rb_sunken:
if (rbSunken.isChecked())
rgManholeStateTwo.clearCheck();
break;
}
}
}
private class OnMyManholeStateTwoCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int position) {
switch (position) {
case R.id.rb_occupation:
if (rbOccupation.isChecked())
rgManholeStateOne.clearCheck();
break;
case R.id.rb_damage:
if (rbDamage.isChecked())
rgManholeStateOne.clearCheck();
break;
case R.id.rb_heave:
if (rbHeave.isChecked())
rgManholeStateOne.clearCheck();
break;
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助。