关于RadioButton注意事项

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/android_I_t/article/details/73771100

在RadioGroup中动态添加RadioButton中遇事一二:

1.在设置RadioButton按钮点击的时候,尤其是可能在做网络请求的时候,千万不要用RadioGroup的RadioGroup.check(RadioButtonId)的方法选中按钮,因为这样你会发现,RadioGroup的监听RadioGroup.OnCheckedChangeListener会被多次调用,如果你在里面作网络操作,本来只需一个网络请求,但是会变成多次,很明显这是用户不希望看到的
原因:见2中源码
解决办法在点击的时候用RadioButton的setChecked(true)方法就只会调用RadioGroup监听的方法一次,例句代码如下:

for (int i=0;i < rbCount ;i++){
            RadioButton radioButton = new RadioButton(mContext);
            RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(
                    RadioGroup.LayoutParams.WRAP_CONTENT,60);
            radioButton.setLayoutParams(layoutParams);
            String labelName = "RadioButton"+i;
            radioButton.setId(170802 +i);
            radioButton.setText(labelName);
            radioButton.setTextSize(TypedValue.COMPLEX_UNIT_SP , 14) ;//字体大小
            radioButton.setTextColor(ContextCompat.getColor(mContext,R.color.textView_color_2a));//设置选中/未选中的文字颜色
            radioButton.setButtonDrawable(android.R.color.transparent);//隐藏单选圆形按钮

            //StateListDrawable drawable = new StateListDrawable();
            //drawable.addState(new int[]{ -android.R.attr.state_checked},
            //       new BitmapDrawable(  mContext.getResources() , bitmapIcon) );
            //drawable.addState(new int[]{android.R.attr.state_checked},
                    new BitmapDrawable(  mContext.getResources() , bitmapSel) );
            //radioButton.setBackground( drawable );//动态Selector
            radioButton.setGravity(Gravity.CENTER_VERTICAL);
	    radioButton.setPadding(15, 0, 15,0);

            rgProductLabel.addView(radioButton);//将单选按钮添加到RadioGroup中

            if (i==0)
                radioButton.setChecked(true);

        }

2.如果你发现你的RadioGroup中的按钮不能互斥了,即:RadioGroup中的有三个RadioButton,在测试点击的时候发现会两个RadioButton按钮的RadioButton.isChecked()都为true,在RadioGoup中这是不允许发生的,什么会导致这样的情况发生那?
原因你的RadioButton没有设置ID或者设置ID的时候与其他组件冲突,导致你的RadioButton在RadioGoup中监听不到对应ID的RadioButton,源码中就是根据你的ID来的
解决方法必须给每个RadioButton赋一个ID才可以互斥,RadioGroup最好也设置一个ID
源码奉上(RadioGroup的check方法 )

public void check(@IdRes int id) {
        // don't even bother
        if (id != -1 && (id == mCheckedId)) {
            return;//如果点的是已经选中的
        }

        if (mCheckedId != -1) {
            setCheckedStateForView(mCheckedId, false);
        }

        if (id != -1) {
            setCheckedStateForView(id, true);
        }

        setCheckedId(id);
    }
	private void setCheckedId(@IdRes int id) {
        mCheckedId = id;
        if (mOnCheckedChangeListener != null) {
            mOnCheckedChangeListener.onCheckedChanged(this, mCheckedId);
        }
    }

    private void setCheckedStateForView(int viewId, boolean checked) {
        View checkedView = findViewById(viewId);
        if (checkedView != null && checkedView instanceof RadioButton) {
            ((RadioButton) checkedView).setChecked(checked);
        }
    }

 
 





猜你喜欢

转载自blog.csdn.net/android_I_t/article/details/73771100