Android动态生成RadioButton

MainActivity


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.ui.FlowRadioGroup;

public class MainActivity extends AppCompatActivity {


    private String radioSelect;
    private String radioNum = "9";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.textView);

        flowRadioGroup = (FlowRadioGroup) findViewById(R.id.testRD);


        //对radioGroup添加监听事件,然后获取到选中的值
        flowRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {

                radioButton = (RadioButton) flowRadioGroup.findViewById(checkedId);
                radioSelect = radioButton.getText().toString();

                Toast.makeText(getApplicationContext(), checkedId + "-----" + radioSelect, Toast.LENGTH_SHORT).show();

            }
        });

    }


    private FlowRadioGroup flowRadioGroup;
    private RadioButton radioButton;
    private String RDname;

    private TextView textView;

    //对RadioGroup添加radioButoon
    public void radioSelect(View view) {

        Log.i("_______", "111111111111");
        //  int intRadio = Integer.parseInt(radioNum);

        int intRadio = Integer.valueOf(radioNum);

        for (int i = 1; i <= intRadio; i++) {

            Log.i("_______", "2222222222222");

            RadioButton radioButton = new RadioButton(MainActivity.this);

            radioButton.setText("jia 1" + i);
            flowRadioGroup.addView(radioButton);
        }
    }


    //取radioButton的值
    public void textRadio(View view) {
        if (radioSelect == null) {
            Toast.makeText(getApplicationContext(), "请选择RadioButton之后再确认", Toast.LENGTH_SHORT).show();
        } else {
            textView.setText(radioSelect);
        }
    }


    //删除所有ridioButton
    public void clearRdio(View view) {
        flowRadioGroup.removeAllViews();
        radioSelect = null;

    }


}

FlowRadioGroup


import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RadioGroup;

/**
 * Created by chenlei on 2016-8-12.
 */
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);
            }
        }
    }

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值