关于Android的RadioGroup

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

由于项目中有用到RadioGroup,所以写了个demo,研究了下RadioGroup。

下面是布局文件:




    

页面代码:

public class MainActivity extends Activity {

    RadioGroup radioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        setRadio();
        //RadioGroup中radiobutton切换监听
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                Log.d("Test", "The checked radio button's id is: " + group.getCheckedRadioButtonId());
                RadioButton radioButton = (RadioButton) group.findViewById(group.getCheckedRadioButtonId());
                Log.d("Test",  "The checked radio button's text is: " + radioButton.getText());
            }
        });
    }

    //根据获取的数据,动态添加radiobutton
    private void setRadio() {
        RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        for (int i=0; i<10; i++) {
            RadioButton radioButton = new RadioButton(this);
            radioButton.setId(i);
            radioButton.setText("Test" + i);
            radioButton.setLayoutParams(params);
            //默认选中第一个radiobutton
            if (i == 0) {
                radioButton.setChecked(true);
            }
            radioGroup.addView(radioButton);
        }
    }
}

写完后运行项目,会发现由于RadioGroup没有自动换行的功能,所以导致如果选项过多的话会显示不全的情况:

关于Android的RadioGroup_第1张图片

然后顺便网上copy了一份关于RadioGroup自动换行的代码:

public class FlowRadioGroup extends RadioGroup {

    public FlowRadioGroup(Context context) {
        super(context);
    }

    public FlowRadioGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int maxWidth = MeasureSpec.getSize(widthMeasureSpec);
        int childCount = getChildCount();
        int x = 0;
        int y = 0;
        int row = 0;

        for (int index = 0; index < childCount; index++) {
            final View child = getChildAt(index);
            if (child.getVisibility() != View.GONE) {
                child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
                // 此处增加onlayout中的换行判断,用于计算所需的高度
                int width = child.getMeasuredWidth();
                int height = child.getMeasuredHeight();
                x += width;
                y = row * height + height;
                if (x > maxWidth) {
                    x = width;
                    row++;
                    y = row * height + height;
                }
            }
        }
        // 设置容器所需的宽度和高度
        setMeasuredDimension(maxWidth, y);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int childCount = getChildCount();
        int maxWidth = r - l;
        int x = 0;
        int y = 0;
        int row = 0;
        for (int i = 0; i < childCount; i++) {
            final View child = this.getChildAt(i);
            if (child.getVisibility() != View.GONE) {
                int width = child.getMeasuredWidth();
                int height = child.getMeasuredHeight();
                x += width;
                y = row * height + height;
                if (x > maxWidth) {
                    x = width;
                    row++;
                    y = row * height + height;
                }
                child.layout(x - width, y - height, x, y);
            }
        }
    }
}

将布局文件中的RadioGroup替换成上面的FlowRadioGroup,再运行项目就能自动换行了




转载于:https://my.oschina.net/u/2459282/blog/645274

你可能感兴趣的:(关于Android的RadioGroup)