RadioGroup同时选中两个RadioButton的问题

当首次点击第二个RadioButton,可以触发onCheckedChanged回调方法,

这时点击其他的RadioButton,然后再次点击第二个的时候,却不会触发回调方法。

       // 初始化
        private void initView() {
            //定义控件ID
            int ID_RADIOGROUP = 1;  
            int ID_HIGHLIGHT = 2;
            
            //定义相对布局
            parentLayout_ = new RelativeLayout(empView_);
            parentLayout_.setBackgroundColor(Color.rgb(125, 35, 25));
            parentLayout_.setLayoutParams(new RelativeLayout.LayoutParams(-1, -1));
            
            // 初始化RadioGroup
            radioGroup_ = new RadioGroup(empView_);
            
            radioGroup_.setId(ID_RADIOGROUP);
            radioGroup_.setOrientation(RadioGroup.HORIZONTAL);
            radioGroup_.setOnCheckedChangeListener(this);
            int radioGroupW = 0;
            // 初始化RadioGroup下的子元素
            if (null != titles_) {
                int size = titles_.length;
                radioGroupW = (int) (itemWidth_ * size);
                for (int i = 0; i < size; i++) {
                    RadioButton rdbtn = (RadioButton) LayoutInflater.from(empView_).inflate(R.layout.tabmenu_radiobutton, null);
                    rdbtn.setLayoutParams(new LinearLayout.LayoutParams((int) itemWidth_, Utils.getScaledValueY(60)));
                    rdbtn.setId(i);
                    rdbtn.setText(titles_[i]);
                    radioGroup_.addView(rdbtn);
                }
            }
            RelativeLayout.LayoutParams rgpm = new RelativeLayout.LayoutParams(radioGroupW, Utils.getScaledValueY(65));
            rgpm.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            
            currentLeft = getCurrentLeft();
            
            // 初始化底部高亮视图
            hiliView_ = new ImageView(empView_);
            hiliView_.setId(ID_HIGHLIGHT);
            RelativeLayout.LayoutParams hilipm = new RelativeLayout.LayoutParams(Utils.getScaledValueX(100), Utils.getScaledValueY(4));
            hilipm.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            hiliView_.setBackgroundColor(0xff33b5e5);
            
            // 把视图添加到相对布局
            parentLayout_.addView(radioGroup_, rgpm);
            parentLayout_.addView(hiliView_, hilipm);
            
            // 把相对布局添加到最外层
            addView(parentLayout_);
            
            RadioButton rbtn = (RadioButton)radioGroup_.getChildAt(1);
            rbtn.setChecked(true);
        }

       /**
         * RadioGroup点击CheckedChanged监听
         */
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            selectedIndex_ = checkedId;
            AnimationSet animationSet = new AnimationSet(true);
            TranslateAnimation translateAnimation = new TranslateAnimation(currentLeft, itemWidth_ * checkedId, 0f, 0f);
            animationSet.addAnimation(translateAnimation);
            animationSet.setFillBefore(false);
            animationSet.setFillAfter(true);
            animationSet.setDuration(100);
            // 开始上面蓝色横条图片的动画切换
            hiliView_.startAnimation(animationSet);
            // 更新当前蓝色横条距离左边的距离
            currentLeft = getCurrentLeft();
            smoothScrollTo((int) currentLeft - (int)itemWidth_, 0);
        }


经过分析,终于找到了答案:

因为RadioGroup和第二个RadioButton的id相同。系统根据id=1去寻找控件时,当找到父类RadioGroup便会返回,并不会寻找它的子视图。所以始终没有触发回调方法。





























你可能感兴趣的:(RadioGroup同时选中两个RadioButton的问题)