Android动态生成RadioButton
16lz
2021-12-04
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(安卓)ListView使用
- scrollView的fillviewport
- Android(安卓): 输入设备键值从底层到应用层的映射流程
- Android下添加新的自定义键值和按键处理流程
- Android之使用GPS和NetWork定位
- Android(安卓)获得View截屏最优方案
- Caused by: org.gradle.api.internal.plugins.PluginApplication
- android 瀑布流