RadioGroup的多行显示RadioButton

首先布局:布局为一个竖向的Linearlayout里放两个RadioGroup,如下

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
 
    android:layout_marginTop="10dp"
    android:layout_marginLeft="20dp"
    android:orientation="vertical"
    android:id="@id/move_radio">
    <RadioGroup
        android:id="@+id/radiogroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rbPack"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="包装ffff" />

        <RadioButton
            android:id="@+id/rbBulk"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="散装dddd" />

        <RadioButton
            android:id="@+id/rbStorageWayOther"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="其它ddd" />
    </RadioGroup>
    <RadioGroup
        android:id="@+id/radiogroup2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rbPack1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="港到门" />

        <RadioButton
            android:id="@+id/rbBulk1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="门到港" />

        <RadioButton
            android:id="@+id/rbStorageWayOther1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="门到门" />
    </RadioGroup>

</LinearLayout>
其次是Activity的核心代码:如下(isSelect是定义的一个全局布尔变量,初始为false)

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
    switch (group.getId()){
        case R.id.radiogroup1:
            if (group != null && checkedId > -1 && !isSelect){
                isSelect = true;
                radioGroup2.clearCheck();
                isSelect = false;
                //获取变更后的选中项的ID
                int radioButtonId = group.getCheckedRadioButtonId();
                //根据ID获取RadioButton的实例
                RadioButton rb = (RadioButton)v.findViewById(radioButtonId);
                System.out.println("---text: "+rb.getText());
            }
            break;
        case R.id.radiogroup2:
            if (group != null && checkedId > -1 && !isSelect){
                isSelect = true;
                radioGroup1.clearCheck();
                isSelect = false;
                //获取变更后的选中项的ID
                int radioButtonId = group.getCheckedRadioButtonId();
                //根据ID获取RadioButton的实例
                RadioButton rb = (RadioButton)v.findViewById(radioButtonId);
                System.out.println("---text: "+rb.getText());
            }
            break;
    }
}
如此,便可达到RadioGroup的多行显示了,今天的收获希望对大家有用,这也是我的第一篇博客,希望以后有更多的好资源和大家分享。。。。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值