常用控件系列之——代码实现RadioGroup嵌套RadioButton

本文介绍了一种在Android中使用硬编码实现RadioGroup嵌套RadioButton的方法,特别针对支付宝、微信及动态银行卡选项的需求。通过LayoutInflater加载RadioButton并利用addView方法指定位置,实现了支付方式的优先级。

今天来为大家分享一下如何使用硬编码的形式实现RadioGroup嵌套RadioButton;

这样的场景,大家在项目中也会很常见,

下面这段代码实现了这样一个需求。在一个单选框的群组中,有下面几个选项。

其中,支付宝、微信是根据已有的sdk固定的数据,但是银行卡的数量是动态可变的。且,银行卡的选项的位置是在微信的上面,意思就是支付方式要优先于微信,

那么,如何在一个radiogroup中来实现指定position的动态控件添加呢。


代码如下:这是xml中,只放置一个空的父容器。其它的功能全部用编码实现。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.shanhaishu.com.radiogroupdemo.MainActivity"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"/>
    <RadioGroup
        android:id="@+id/rg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       >
    </RadioGroup>

</LinearLayout>

MainActivity中代码:

说明:控制器中分三段代码,第二段是布局实现,第三段是点击测试使用。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    rg = (RadioGroup) findViewById(R.id.rg);
    rg.setOnCheckedChangeListener(this);
    addRadioButton();

}

注意使用了引用style的方式来初始化radiobutton, style大家可以按自己的需求来随便定义。

使用了addview的position。来指定控件出现的位置;

需要给每一个控件,设置ID,不然会出现重复选中;

private void addRadioButton() {
    int j = 0;
    rb1 = (RadioButton) LayoutInflater.from(this).inflate(R.layout.radiobutton_style, null);
    rb1.setText("支付宝1");
    rb1.setId(j);
    rg.addView(rb1, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    rb2 = (RadioButton) LayoutInflater.from(this).inflate(R.layout.radiobutton_style, null);
    rb2.setText("微信");
    rb2.setId(j + 1);
    rg.addView(rb2, 1, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    for (int i = 2; i < 4; i++) {
        RadioButton radioButton = (RadioButton) LayoutInflater.from(this).inflate(R.layout.radiobutton_style, null);

        radioButton.setText("我是银行卡" + i);
        radioButton.setId(j + i);
        rg.addView(radioButton, i, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

    }
    rb3 = (RadioButton) LayoutInflater.from(this).inflate(R.layout.radiobutton_style, null);
    rb3.setText("微信");
    rb1.setId(j + 4);
    rg.addView(rb3, 4, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    rg.invalidate();
}

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
    if (rb1.isChecked()) {
        categoty = ZHIFUBAO;
        Log.e("tag:", categoty);
    } else if (checkedId == rb2.getId()) {
        categoty = WEIXIN;
        Log.e("tag:", categoty);
    } else if (checkedId == rb3.getId()) {
        categoty = YINLIAN;
        Log.e("tag:", categoty);
    } else {
        categoty = KJZF;
        Log.e("tag:", categoty);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值