RadioButton自定义样式及点击事件全解析
来源:互联网 发布:sql计算总和 编辑:程序博客网 时间:2024/06/15 17:42
1.设置文字在下边的RadioButton
<RadioButton android:id="@+id/rb_male" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="50dp" android:text="男孩" android:button="@null" android:gravity="center" android:textColor="@color/black" android:drawableTop="@drawable/ic_launcher" android:checked="true" />
主要代码为 android:button="@null"
、android:drawableTop="@drawable/ic_launcher"
可以看到 drawableTop、drawableBottom、drawableLeft、drawableRight 可以用来随意设置位置来实现文字的位置。
2.设置RadioButton选中时的样式
在drawable中创建一个select.xml,加入以下代码即可。
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/ic_select_sex_male_p" android:state_checked="true" /> <item android:drawable="@drawable/ic_select_sex_male_n" android:state_checked="false" /></selector>
使用方法
<RadioButton android:id="@+id/rb_male" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="50dp" android:button="@null" android:checked="true" android:drawableTop="@drawable/select" android:gravity="center" android:text="男孩" android:textColor="@color/black" />
3.建立RadioGroup中的RadioButton
初始化控件
private void setUpView() { rg_sex = (RadioGroup) findViewById(R.id.rg_sex); rb_sex_male = (RadioButton) findViewById(R.id.rb_sex_male); rb_sex_female = (RadioButton) findViewById(R.id.rb_sex_female); }
获取对应RadioButton的点击事件(默认值可以设置)
private void setRadioGroupOnCheckedChangeListener() { rg_sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if (checkedId == rb_sex_male.getId()) { } else (checkedId == rb_sex_female.getId()) { } } }); }
xml布局
<RadioGroup android:id="@+id/rg_sex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center" android:orientation="horizontal" > <RadioButton android:id="@+id/rb_sex_male" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="50dp" android:button="@null" android:checked="true" android:drawableTop="@drawable/a" android:gravity="center" android:text="男孩" android:textColor="@color/black" /> <RadioButton android:id="@+id/rb_sex_female" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="50dp" android:button="@null" android:drawableTop="@drawable/b" android:text="女孩" android:textColor="@color/black" /> </RadioGroup>
1 0
- RadioButton自定义样式及点击事件全解析
- RadioButton - android 自定义RadioButton样式
- RadioButton点击事件
- 自定义RadioButton样式1
- 自定义RadioButton样式
- RadioButton自定义样式
- android 自定义RadioButton样式
- 自定义RadioButton样式2
- 自定义RadioButton样式
- 自定义RadioButton样式2
- android 自定义RadioButton样式
- Android 自定义RadioButton样式
- 如何自定义RadioButton样式
- Android自定义RadioButton样式
- android 自定义RadioButton样式
- 如何自定义RadioButton样式
- radiobutton设置自定义样式
- android 自定义RadioButton样式
- OpenGL ES 2.0 Shader总结
- shiro学习和使用实例(1)
- NetBeans IDE 中国教育考试版(2007)使用说明和演示教程
- spring_学习笔记
- iOS工作之余随记--Apple Pay 线上支付接入简介
- RadioButton自定义样式及点击事件全解析
- getBuiltInDisplay
- js遍历拼接list集合对象,JSONArray数组
- Eventbus 学习完毕后的总结
- error: cannot parse version string: kg01
- 检查型异常和不检查型异常
- 剑指offer代码解析——面试题24二叉搜索树的后序遍历序列
- Core Animation - 变换<二>
- 51NOD 1163——最高的奖励(可用优先队列解决的贪心问题)