解决RadioGroup多行显示,单选问题

效果如下:

解决RadioGroup多行显示,单选问题_第1张图片
QQ图片20170224110543.png
解决RadioGroup多行显示,单选问题_第2张图片
QQ图片20170224110557.png

RadioGroup多行显示其实就是两个RadioGroup进行切换,使用RadioGroup的clearCheck()方法进行操作

1、布局如下:

                  

                        

                        

                        
                    
                 

                    

                    

                    
                


2、具体操作如下:

声明控件:


    /**
     * 完好
     */
    @InjectView(R.id.rb_intact)
    RadioButton rbIntact;
    /**
     * 丢失
     */
    @InjectView(R.id.rb_lose)
    RadioButton rbLose;
    /**
     * 凹陷
     */
    @InjectView(R.id.rb_sunken)
    RadioButton rbSunken;
    @InjectView(R.id.rg_manhole_state_one)
    RadioGroup rgManholeStateOne;
    /**
     * 占用
     */
    @InjectView(R.id.rb_occupation)
    RadioButton rbOccupation;
    /**
     * 损坏
     */
    @InjectView(R.id.rb_damage)
    RadioButton rbDamage;
    /**
     * 凸起
     */
    @InjectView(R.id.rb_heave)
    RadioButton rbHeave;
    @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;
            }
        }
    }

你可能感兴趣的:(解决RadioGroup多行显示,单选问题)