Android动态生成RadioButton
16lz
2021-01-23
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); } } }}
更多相关文章
- android获得屏幕高度和宽度
- Android 获取View高度宽度
- Android——TabHost(标签容器)相关知识总结贴
- 关于LinearLayout布局中,子控件平分宽度
- Android获取屏幕实际高度跟显示高度,判断Android设备是否拥有虚拟
- Android Webview适配屏幕宽度
- Android布局容器
- Android中ListVIew高度自适应,解决ScrollView冲突问题以及Android