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

来源:互联网 发布:java初级编程题目 编辑:程序博客网 时间:2024/05/29 07:10

今天来为大家分享一下如何使用硬编码的形式实现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中代码:

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

@Overrideprotected 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();}

@Overridepublic 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);    }}


0 0
原创粉丝点击