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. centos7 防火墙

    1.firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: systemctl status f ...

  2. 128、View 绘制流程 & 自定义View

    记清楚函数调用的顺序才能准确地进行调用. 根据调用链,可将整个绘制过程分为三部分:Measure - Layout - Draw Measure 过程 1. 测量过程由上至下,在measure过程的最 ...

  3. 安装redis出现cc adlist.o /bin/sh:1:cc:not found的解决方法

    安装redis时 提示执行make命令时提示 CC adlist.o /bin/sh: cc: 未找到命令   问题原因:这是由于系统没有安装gcc环境,因此在进行编译时才会出现上面提示,当安装好gc ...

  4. docker 怎么下载指定版本的镜像文件

    在使用Docker时我想pull远端仓库的CentOS 6的镜像,但是我直接搜索下载下来的是最新的版本.那我有没有什么办法直接下载Centos6呢? 方法: 直接上 hub.docker.com 查 ...

  5. redis(一)--认识redis

    Redis官网对redis的定义是:“Redis is an open source, BSD licensed, advanced key-value cache and store”,可以看出,R ...

  6. 洛谷P1029 最小公约数和最大公倍数问题【数论】

    题目:https://www.luogu.org/problemnew/show/P1029 题意: 给定两个数$x$和$y$,问能找到多少对数$P$$Q$,使得他们的最小公约数是$x$最大公倍数是$ ...

  7. html学习_认识html

    1.HTML骨架 <html>----根标签 <head>---头标签 </head> <body>---主体标签 </body> < ...

  8. linux:基本指令touch, cp 和 mv

    touch 新建 #touch 的使用很简单, 我们先去往 Documents 的文件夹, 里面已经有了 folder1 和 file1, 如果我们想新建一个 file2 使用下面的语句就好. 一个空 ...

  9. 解决sudo: npm: command not found

    sudo ln -s /opt/node-v11.4.0/bin/npm /usr/bin/npm sudo ln -s /opt/node-v11.4.0/bin/node /usr/bin/nod ...

  10. iOS 模拟器运行不能联网 PAC Fetch failed with error

    app在模拟器是哪个启动成功会自动连接服务器,然后Xcode控制台报错, 模拟器 PAC Fetch failed with error [NSURLErrorDomain:-1001] 这类问题有好 ...