Android RadioGroup多行顯示,解決單選問

艾陽丶發表於2017-04-19

導語

如下圖所示,這是一個導航選擇彈框。進行單項選擇,然後會監聽回撥選擇的事件。問題是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;
            }
        }
    }

 

 

 

 

相關文章