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. android 监听联系人数据库
  2. Android(安卓)ListView使用
  3. scrollView的fillviewport
  4. Android(安卓): 输入设备键值从底层到应用层的映射流程
  5. Android下添加新的自定义键值和按键处理流程
  6. Android之使用GPS和NetWork定位
  7. Android(安卓)获得View截屏最优方案
  8. Caused by: org.gradle.api.internal.plugins.PluginApplication
  9. android 瀑布流

随机推荐

  1. Android可显示数字进度的进度条使用教程
  2. ToolBar介绍
  3. Android读书笔记---View基础
  4. Android(安卓)实现自定义Spinner
  5. 配置android jni开发环境
  6. Appium环境搭建
  7. apktool之APK解压、打包
  8. Android(安卓)串口通信开发笔记3:CMake 方
  9. Android display架构分析-SW架构分析(1-4
  10. Android技术框架概要