RadioButton和CheckBox差不多,这里只写一个,因为我本身不是学andorid,所以就当给自己留一个备份,省的每次用到都需要代码敲一次,很麻烦
1.如果想选中时想改变颜色可以设置一个xml,我设置的upload_state_btn
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="#0000ff"></item>
    <item android:color="#2a2a2a"></item>
</selector>

 使用

<RadioGroup android:layout_width="match_parent"
android:layout_height="44dp"
android:orientation="horizontal"
android:id="@+id/group">
<RadioButton android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="男"
android:button="@null"
android:gravity="center"
android:checked="true"
android:enabled="false"
android:textColor="@drawable/upload_state_btn"
android:id="@+id/btn1"
/>
<RadioButton android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="女"
android:button="@null"
android:gravity="center"
android:textColor="@drawable/upload_state_btn"
android:id="@+id/btn2"/>
</RadioGroup>

点击事件有两种方法

方法一:

  RadioGroup group = (RadioGroup)findViewById(R.id.group);
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.btn1:
break;
case R.id.btn2:
break;
}
}
});

方法二:

   RadioButton btn1;
RadioButton btn2;
btn1 = (RadioButton)findViewById(R.id.btn1);
btn2 = (RadioButton)findViewById(R.id.btn2); btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent startIntent = new Intent(DemoActivity.this, MyService.class);
//startService启动形式
startService(startIntent); }
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent startIntent = new Intent(DemoActivity.this, MyService.class);
//startService启动形式
stopService(startIntent);
}
});

如果需要改变系统自带的图标,可以添加xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@mipmap/checkbox_role"
android:state_checked="true"/>
<item
android:drawable="@mipmap/checkbox_dis_role"
android:state_checked="false"/>
<item
android:drawable="@mipmap/checkbox_dis_role"/>
</selector>

在styles中添加:

 <style name="CustomTheme" parent="@android:style/Widget.CompoundButton.RadioButton">
<item name="android:button">@drawable/custom_radio</item>
</style>

使用的使用直接在xml RadioButton属性中添加一句:

 style="@style/CustomTheme"

RadioButton使用的更多相关文章

  1. RadioButton与CheckBox

    笔者长期从事于数据库的开发,算了,不提当年了,因为一直用的是小语种(PowerBuilder),还是来说说这两个最常见的控件吧! RadioButton(单选)和CheckBox(多选) 先来看看继承 ...

  2. 【WPF】值是枚举的RadioButton 绑定问题

    源 1.RadioButton 2.IValueConverter 3.枚举 xaml实现 <RadioButton Content="单打热身" GroupName=&qu ...

  3. Android RadioGroup和RadioButton详解

    实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用.RadioGroup是单选组合框,可以容纳多个RadioButton的容器.在没有RadioGrou ...

  4. react native RadioButton(单选按钮)

    刚刚写完这个多选按钮,我觉得没有单选的话,总会觉得有一点点不爽,因为在项目中我也没有用到单选,所以我没有好好研究源码,所以我在Github上找了一下,发现有一个挺好的,简单,不花哨. 在Github上 ...

  5. RadioButton(单选按钮)文字在按钮的左边

    <RadioButton style="@style/CustomCheckboxTheme" android:layout_width="fill_parent& ...

  6. RadioGroup、RadioButton、CheckBox、Toast用法

    xml布局文件如下: <RadioGroup android:id="@+id/sex" android:layout_width="wrap_content&qu ...

  7. 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch

    [源码下载] 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch 作者:webabcd 介绍背水一 ...

  8. 转 Android RadioButton设置选中时文字和背景颜色同时改变

    主要应用在购物车,像淘宝的那样,点击以后弹出一个选择种类颜色这样的popuwindow以后,然后这个选择种类的地方要用到类似这个玩意儿. 搜了一下,效果和这个文章一致.转了. 原文地址:http:// ...

  9. WPF中RadioButton绑定数据的正确方法

    RadioButton一般用于单选的时候,也就是从一组值中选择一个值. 比如性别有“男”和“女”两种取值,而对于一个员工的实例来说,性别的取值要么是男,要么是女. 这种时候一般就会用到RadioBut ...

  10. Android-组件RadioButton使用技巧

    当初第一次接触Android组件的时候,我感觉微信底部的菜单,是用button这样的组件来做的. 可我想错了.却是用RadioButton来做的.那到底怎么做,接下来我就做一下分享!希望看了之后你也觉 ...

随机推荐

  1. Android studio启动后卡在refreshing gradle project

    这个问题几乎每个刚使用Android studio的同学都会碰到过,网上有各式各样的方法,有的说使用本地gradle,我试过多次,每次启动Android studio时还是会检查更新,所以根本上解决的 ...

  2. POJ 2018 Best Cow Fences(二分+最大连续子段和)

    Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14601 Accepted: 4720 Desc ...

  3. iOS - 富文本直接设置文字的字体大小和颜色

    富文本效果图: 富文本实现代码: UILabel *orderSureLabel = [Common lableFrame:CGRectZero title:] textColor:[UIColor ...

  4. css---颜色过渡渐变

    CSS---颜色过渡渐变 <!doctype html> <html lang="en"> <head> <meta charset=&q ...

  5. python 中的 print 函数与 list函数

    print()  函数: 传入单个参数时默认回车换行,关键词 end 可以用来避免输出后的回车(换行), 或者以一个不同的字符串结束输出. >>> a, b = 0, 1 >& ...

  6. mysql 登录远程数据库 失败

    配置mysql结束,本机能够成功登录和查询. 但其他主机使用mysql -h*** -P3306 -u*** -p***,登录数据库. centos7上反馈:ERROR 2003 (HY000): C ...

  7. Red Hat6设置使用CentOS的yum源

    环境查看 red hat系统使用自己默认的yum源未注册在使用yum安装软件的时候会出现以下错误提示 可以修改成centos的yum源 卸载yum软件 rpm -qa|grep yum|xargs r ...

  8. 电子产品使用感受之--Windows 10 小米笔记本Air HDMI转VGA无信号问题

    最近一直通过HDMI转VGA线缆链接我的戴尔P2314H显示器,前天睡觉前,看到电脑上英伟达显卡推了驱动更新,顺手更新了一下,就去睡觉了,转天晚上再用,HDMI接口就没有信号了,上网查了一些信息,获知 ...

  9. 网卡配置文件详解 用户管理与文件权限篇 文件与目录权限 软连接 tar解压命令 killall命令 linux防火墙 dns解析设置 计划任务crond服务 软件包安装 阿里云 yum源 安装

    Linux系统基础优化及常用命令 Linux基础系统优化 引言没有,只有一张图. Linux的网络功能相当强悍,一时之间我们无法了解所有的网络命令,在配置服务器基础环境时,先了解下网络参数设定命令. ...

  10. cc2640 细节展示

    1. 对于 cc2640内部有两个单片机,一个m3负责内核,另一个是一个16位单片机,应该是msp430可以替代主机完成一些数据采集,adc采集,iic等等功能,传感器软件内部可以进行外设配置,并使用 ...