RadioGroup、RadioButton动态创建并定制icon显示

由于原生的Radiobutton不能满足业务需求,所以需要自己定制icon图片,和默认选中某一项。需要自己代码动态实现。废话不多说,上代码:

private void setViewData() {
        radioGroup.removeAllViews();

        int margin = ScreenUtil.dip2px(this, 14.0f);
        int marginLeft = ScreenUtil.dip2px(this, 20.0f);
        int paddingLeft = ScreenUtil.dip2px(this, 3.0f);

        RadioGroup.LayoutParams layoutParams = 
                new RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        RadioGroup.LayoutParams layoutParams1 = 
                new RadioGroup.LayoutParams(ViewGroup
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,可以通过RadioGroup获取选中的RadioButton。可以通过以下步骤实现: 1. 在布局文件中添加RadioGroupRadioButton控件,例如: ``` <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 1" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 2" /> <RadioButton android:id="@+id/radioButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 3" /> </RadioGroup> ``` 2. 在Activity或Fragment中获取RadioGroup对象,并设置监听器,例如: ``` RadioGroup radioGroup = findViewById(R.id.radioGroup); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton radioButton = findViewById(checkedId); if (radioButton != null) { String text = radioButton.getText().toString(); // Do something with the selected text } } }); ``` 在监听器的回调方法中,我们可以通过checkedId获取选中的RadioButton的id,然后通过findViewById方法获取该RadioButton对象,最后通过该对象的getText方法获取选中的文本内容。 注意,如果RadioGroup中没有选中任何RadioButton时,checkedId的值为-1。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值