RadioGroup动态添加RadioButton,并且获得事件
2016-01-11 10:58
579 查看
原先的xml
由于有许多的RadioButton是动态的,不是固定的一些,所以需要在代码中,动态的添加到RadioGroup中,下面是我的实现方法。
1、添加RadioButton到RadioGroup中
[java] view
plain copy
RadioGroup group;
for(int i=0; i<10; i++)
{
RadioButton tempButton = new RadioButton(this);
tempButton.setBackgroundResource(R.drawable.xxx); // 设置RadioButton的背景图片
tempButton.setButtonDrawable(R.drawable.xxx); // 设置按钮的样式
tempButton.setPadding(80, 0, 0, 0); // 设置文字距离按钮四周的距离
tempButton.setText("按钮 " + i);
group.addView(tempButton, LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
}
2、为RadioGroup添加事件处理,可以得到当前选择的RadioButton
[java] view
plain copy
group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
RadioButton tempButton = (RadioButton)findViewById(checkedId); // 通过RadioGroup的findViewById方法,找到ID为checkedID的RadioButton
// 以下就可以对这个RadioButton进行处理了
}
});
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentBottom="true" android:background="#ffffff" android:orientation="vertical" > <com.lvche.origin.view.HomeActivityTitleView android:id="@+id/homeTitle" android:layout_width="match_parent" android:layout_height="45dp" android:background="#fff888" /> <ScrollView android:layout_width="match_parent" android:scrollbars="none" android:layout_height="wrap_content" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:ignore="ScrollViewSize" > <!-- 滚动 轮播图片 --> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:orientation="vertical" > <com.lvche.origin.activity.slide_imgs.SlideViewPager android:id="@+id/slide_viewpager" android:layout_width="match_parent" android:layout_height="150dp" android:background="#fff888" /> <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/slide_viewpager" android:layout_centerHorizontal="true" android:button="@drawable/btn_radio_holo_light1" android:clickable="false" android:orientation="horizontal" > <RadioButton android:id="@+id/radio0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:button="@drawable/btn_radio_holo_light1" android:checked="true" android:clickable="false" /> <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:button="@drawable/btn_radio_holo_light1" android:clickable="false" /> <RadioButton android:id="@+id/radio2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:button="@drawable/btn_radio_holo_light1" android:clickable="false" /> <RadioButton android:id="@+id/radio3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:button="@drawable/btn_radio_holo_light1" android:clickable="false" /> <RadioButton android:id="@+id/radio4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:button="@drawable/btn_radio_holo_light1" android:clickable="false" /> </RadioGroup> </RelativeLayout> <!-- 滚动 轮播图片 end --> <!-- 地图导航 --> <LinearLayout android:layout_width="match_parent" android:layout_height="58dp" android:orientation="horizontal" > <ImageView android:layout_width="35dp" android:layout_height="35dp" android:layout_gravity="center_vertical" android:layout_marginLeft="25dp" android:background="@drawable/logo_pgg" /> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:layout_gravity="center_vertical" android:layout_marginLeft="25dp" android:background="@drawable/logo_pgg" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp" android:text="020国际皮革五金城导航" tools:ignore="HardcodedText" /> </LinearLayout> <ImageView android:layout_width="fill_parent" android:layout_height="1dp" android:background="#aaaaaa" /> <LinearLayout android:layout_width="match_parent" android:layout_height="58dp" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="0.21" android:src="@drawable/correcting_border" android:gravity="center_vertical|center_horizontal" android:text="-1F" android:textColor="#ffffff" android:textSize="25sp" android:background="#555fff" android:textStyle="bold" /> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="0.68" android:orientation="vertical" android:paddingLeft="6dp" > <TextView android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_weight="0.59" android:gravity="center_vertical" android:text="富一层" android:textColor="#555555" android:textSize="18sp" tools:ignore="HardcodedText" /> <TextView android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_weight="1" android:gravity="center_vertical" android:textColor="#999999" android:textSize="12sp" android:text="羊皮、山东大皮、进口头层" tools:ignore="HardcodedText" /> </LinearLayout> <ImageView android:layout_width="0dp" android:layout_height="25dp" android:layout_gravity="center_vertical" android:layout_weight="0.1" android:background="@drawable/logo_pgg" /> </LinearLayout> <ImageView android:layout_width="fill_parent" android:layout_height="1dp" android:background="#aaaaaa" /> <LinearLayout android:layout_width="match_parent" android:layout_height="58dp" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="0.21" android:src="@drawable/correcting_border" android:gravity="center_vertical|center_horizontal" android:text="1F" android:textColor="#ffffff" android:background="#8ffeee" android:textSize="25sp" android:textStyle="bold" /> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="0.68" android:orientation="vertical" android:paddingLeft="6dp" > <TextView android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_weight="0.59" android:gravity="center_vertical" android:text="一层" android:textColor="#555555" android:textSize="18sp" tools:ignore="HardcodedText" /> <TextView android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_weight="1" android:gravity="center_vertical" android:textColor="#999999" android:textSize="12sp" android:text="绵羊皮、山羊皮、超纤、PU革" tools:ignore="HardcodedText" /> </LinearLayout> <ImageView android:layout_width="0dp" android:layout_height="25dp" android:layout_gravity="center_vertical" android:layout_weight="0.1" android:background="@drawable/logo_pgg" /> </LinearLayout> <ImageView android:layout_width="fill_parent" android:layout_height="1dp" android:background="#aaaaaa" /> <LinearLayout android:layout_width="match_parent" android:layout_height="58dp" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="0.21" android:src="@drawable/correcting_border" android:gravity="center_vertical|center_horizontal" android:text="2F" android:background="#00ffaa" android:textColor="#ffffff" android:textSize="25sp" android:textStyle="bold" /> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="0.68" android:orientation="vertical" android:paddingLeft="6dp" > <TextView android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_weight="0.59" android:gravity="center_vertical" android:text="二层" android:textColor="#555555" android:textSize="18sp" tools:ignore="HardcodedText" /> <TextView android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_weight="1" android:gravity="center_vertical" android:textColor="#999999" android:textSize="12sp" android:text="插口、铁线扣、压铸件、其他小配件" tools:ignore="HardcodedText" /> </LinearLayout> <ImageView android:layout_width="0dp" android:layout_height="25dp" android:layout_gravity="center_vertical" android:layout_weight="0.1" android:background="@drawable/logo_pgg" /> </LinearLayout> <ImageView android:layout_width="fill_parent" android:layout_height="1dp" android:background="#aaaaaa" /> <LinearLayout android:layout_width="match_parent" android:layout_height="58dp" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="0.21" android:src="@drawable/correcting_border" android:gravity="center_vertical|center_horizontal" android:text="3F" android:textColor="#ffffff" android:background="#ff00ff" android:textSize="25sp" android:textStyle="bold" /> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="0.68" android:orientation="vertical" android:paddingLeft="6dp" > <TextView android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_weight="0.59" android:gravity="center_vertical" android:text="三层" android:textColor="#555555" android:textSize="18sp" tools:ignore="HardcodedText" /> <TextView android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_weight="1" android:gravity="center_vertical" android:textColor="#999999" android:textSize="12sp" android:text="中空钉、9字钉、其他小配件" tools:ignore="HardcodedText" /> </LinearLayout> <ImageView android:layout_width="0dp" android:layout_height="25dp" android:layout_gravity="center_vertical" android:layout_weight="0.1" android:background="@drawable/logo_pgg" /> </LinearLayout> <ImageView android:layout_width="fill_parent" android:layout_height="1dp" android:background="#aaaaaa" /> <!-- 地图导航 end --> <!-- 广告 --> <LinearLayout android:layout_width="match_parent" android:layout_height="58dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp" android:textColor="#888888" android:text="新品发布" android:textSize="20sp" tools:ignore="HardcodedText" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="150dp" android:orientation="horizontal" android:layout_marginBottom="15dp" > <ImageView android:layout_height="fill_parent" android:layout_width="0dp" android:background="@drawable/logo_pgg" android:layout_weight="0.48" android:layout_marginRight="5dp" /> <LinearLayout android:layout_height="fill_parent" android:layout_width="0dp" android:orientation="vertical" android:layout_weight="0.5"> <ImageView android:layout_height="0dp" android:background="@drawable/e" android:layout_width="fill_parent" android:layout_weight="0.49" android:layout_marginBottom="5dp" /> <ImageView android:layout_height="0dp" android:background="@drawable/a" android:layout_width="fill_parent" android:layout_weight="0.49" /> </LinearLayout> </LinearLayout> <ImageView android:layout_width="match_parent" android:layout_height="80dp" android:background="@drawable/c" android:layout_marginBottom="15dp" /> <ImageView android:layout_width="match_parent" android:layout_height="80dp" android:background="@drawable/c" android:layout_marginBottom="15dp" /> <ImageView android:layout_width="match_parent" android:layout_height="80dp" android:background="@drawable/c" android:layout_marginBottom="15dp" /> <!-- 广告 end --> </LinearLayout> </ScrollView> </LinearLayout>
由于有许多的RadioButton是动态的,不是固定的一些,所以需要在代码中,动态的添加到RadioGroup中,下面是我的实现方法。
1、添加RadioButton到RadioGroup中
[java] view
plain copy
RadioGroup group;
for(int i=0; i<10; i++)
{
RadioButton tempButton = new RadioButton(this);
tempButton.setBackgroundResource(R.drawable.xxx); // 设置RadioButton的背景图片
tempButton.setButtonDrawable(R.drawable.xxx); // 设置按钮的样式
tempButton.setPadding(80, 0, 0, 0); // 设置文字距离按钮四周的距离
tempButton.setText("按钮 " + i);
group.addView(tempButton, LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
}
2、为RadioGroup添加事件处理,可以得到当前选择的RadioButton
[java] view
plain copy
group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
RadioButton tempButton = (RadioButton)findViewById(checkedId); // 通过RadioGroup的findViewById方法,找到ID为checkedID的RadioButton
// 以下就可以对这个RadioButton进行处理了
}
});
相关文章推荐
- Spring3 MVC请求参数获取的几种方法
- 模板、泛型、STL之间的关系
- linux C下的setjmp与longjmp介绍
- C++ 共享指针 shared_ptr
- 缺少编译器要求的成员“System.Runtime.CompilerServices.ExtensionAttribute..ctor” 解决方案
- JS判断图片是否加载完成三种方式
- Java中sleep()和wait()的区别
- ionic复选框应用
- C#委托的介绍(delegate、Action、Func、predicate)
- 31. Next Permutation
- C++常量折叠和类中const、static成员变量初始化
- 【Ionic】---$ionicLoading ion-spinner SVG旋转加载的动画图标
- C++中int,float,string,char*的转换(待续)
- android studio如何使用git
- sql 2008 远程过程调用失败 0x800706be
- “约定优于配置”与Magento改造尝试三之routerName加载
- Linux 下smi/mdio总线通信
- 配置本地git链接远程仓库
- 如何写好年终总结
- 最通俗易懂的经济学文