Android RadioButton设置选中时文字和背景颜色同时改变
来源:互联网 发布:弱电网络模块 编辑:程序博客网 时间:2024/05/16 15:46
在使用 RadioButton 时,有时我们会想要达到选中时文字颜色和背景颜色同时改变的效果,这里还需要多进行几步操作。
首先,在布局文件中新建一组 RadioButton :
- <RadioGroup
- android:layout_width=“match_parent”
- android:layout_height=“wrap_content”
- android:gravity=“center”
- android:orientation=“horizontal”>
- <RadioButton
- android:id=“@+id/btn1”
- android:layout_width=“0dp”
- android:layout_height=“35dp”
- android:layout_weight=“1”
- android:background=“@drawable/radiobutton_background”
- android:button=“@null”
- android:gravity=“center”
- android:text=“P0501”
- android:textColor=“@color/radiobutton_textcolor”
- android:textSize=“14sp” />
- <RadioButton
- android:id=“@+id/btn2”
- android:layout_width=“0dp”
- android:layout_height=“35dp”
- android:layout_marginStart=“10dp”
- android:layout_weight=“1”
- android:background=“@drawable/radiobutton_background”
- android:button=“@null”
- android:gravity=“center”
- android:text=“P0502”
- android:textColor=“@color/radiobutton_textcolor”
- android:textSize=“14sp” />
- <RadioButton
- android:id=“@+id/btn3”
- android:layout_width=“0dp”
- android:layout_height=“35dp”
- android:layout_marginStart=“10dp”
- android:layout_weight=“1”
- android:background=“@drawable/radiobutton_background”
- android:button=“@null”
- android:gravity=“center”
- android:text=“P0503”
- android:textColor=“@color/radiobutton_textcolor”
- android:textSize=“14sp” />
- </RadioGroup>
<RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal"> <RadioButton android:id="@+id/btn1" android:layout_width="0dp" android:layout_height="35dp" android:layout_weight="1" android:background="@drawable/radiobutton_background" android:button="@null" android:gravity="center" android:text="P0501" android:textColor="@color/radiobutton_textcolor" android:textSize="14sp" /> <RadioButton android:id="@+id/btn2" android:layout_width="0dp" android:layout_height="35dp" android:layout_marginStart="10dp" android:layout_weight="1" android:background="@drawable/radiobutton_background" android:button="@null" android:gravity="center" android:text="P0502" android:textColor="@color/radiobutton_textcolor" android:textSize="14sp" /> <RadioButton android:id="@+id/btn3" android:layout_width="0dp" android:layout_height="35dp" android:layout_marginStart="10dp" android:layout_weight="1" android:background="@drawable/radiobutton_background" android:button="@null" android:gravity="center" android:text="P0503" android:textColor="@color/radiobutton_textcolor" android:textSize="14sp" /> </RadioGroup>这里面有三个属性要做一下说明:
1、Android:button=”@null” 这样设置可以不显示我们通常所见的 RadioButton 中的圆形选中按钮.
2、android:background=”@drawable/radiobutton_background” 这里设置了背景选择器,代码如下:
- <selector xmlns:android=“http://schemas.android.com/apk/res/android”>
- <item android:drawable=“@drawable/radiobutton_background_unchecked”
- android:state_checked=“false” />
- <item android:drawable=“@drawable/radiobutton_background_checked”
- android:state_checked=“true” />
- </selector>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/radiobutton_background_unchecked" android:state_checked="false" /> <item android:drawable="@drawable/radiobutton_background_checked" android:state_checked="true" /></selector>这里面的选中样式又指向一个 Drawable 资源文件 radiobutton_background_checked.xml ,具体代码如下:
- <shape xmlns:android=“http://schemas.android.com/apk/res/android”
- android:shape=“rectangle”>
- <!– 填充 –>
- <solid android:color=“@color/color14” />
- <!– 圆角 –>
- <corners android:radius=“5dp” />
- </shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!-- 填充 --> <solid android:color="@color/color14" /> <!-- 圆角 --> <corners android:radius="5dp" /></shape>
以上这些资源文件都放在 res/drawable/ 目录下。
3、android:textColor=”@color/radiobutton_textcolor” 这里设置了字体颜色选择器,需要稍作说明的是:需要在 res 目录下新建一个
文件夹取名为 color ,将字体颜色选择器 radiobutton_textcolor.xml 文件存放在 res/color/ 目录下面。代码如下:
- <selector xmlns:android=“http://schemas.android.com/apk/res/android”>
- <item android:color=“@color/color2”
- android:state_checked=“false” />
- <item android:color=“@color/color1”
- android:state_checked=“true” />
- </selector>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@color/color2" android:state_checked="false" /> <item android:color="@color/color1" android:state_checked="true" /></selector>经过以上步骤后,我们来看一下效果图:
最后提一下怎么通过 RadioGroup 获取 RadioButton :
- RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
- radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(RadioGroup group, int checkedId) {
- RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
- String result = radioButton.getText().toString();
- }
- });
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton radioButton = (RadioButton) group.findViewById(checkedId); String result = radioButton.getText().toString(); } });这样就可以获取到当前 RadioGroup 中选中的 RadioButton ,然后进行一些你想要的操作。
转载自
阅读全文
0 0
- Android RadioButton设置选中时文字和背景颜色同时改变
- Android RadioButton设置选中时文字和背景颜色同时改变
- Android 动态设置RadioButton选中和没选中的颜色及设置默认选中某一项
- RadioGroup的Radiobutton点击时改变字体颜色和背景颜色(采用Xml文件设置)
- RadioGroup 的 RadioButton 选择改变字体颜色和背景颜色(通过XML设置)
- android如何改变listview中item选中时文字的颜色
- 改变listview中item选中时文字的颜色
- 改变listview中item选中时文字的颜色
- 改变listview中item选中时文字的颜色
- RadioGroup 的 RadioButton 选择改变字体颜色和背景颜色
- android 自定义 radiobutton 文字颜色随选中状态而改变
- android 自定义 radiobutton 文字颜色随选中状态而改变
- android 自定义 radiobutton 文字颜色随选中状态而改变
- android 自定义 radiobutton 文字颜色随选中状态而改变
- android 自定义 radiobutton 文字颜色随选中状态而改变
- android 自定义 radiobutton 文字颜色随选中状态而改变
- android radiobutton选中字体颜色改变的方法
- Android Radiobutton 文字颜色随选中状态而改变
- Kinect开发STEP1:KinectSDK1.8在win32控制台程序下的环境配置
- 怎么用网线连接开发板
- Android中的硬件加速
- Java并发编程:线程池的使用
- WPF简单教程:控件ViewBox
- Android RadioButton设置选中时文字和背景颜色同时改变
- Ajax认识
- Android Service和Thread的区别
- TensorFlow MNIST LeNet 模型持久化
- Java数组的遍历与求和
- util.God -2
- 阿里物联网套件-服务端SDK学习实践(基础篇-1准备)
- cookie跨域session共享
- 用两个栈实现队列