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. 在MyEclipse中将Java Project转换成Web Project

    在MyEclipse中将Java Project转换成Web Project 此添加方法是针对MyEclipse中添加的: 编辑工程的.project文件: 添加 <nature>com. ...

  2. Web上传文件的原理及实现

    现在有很多Web程序都有上传功能,实现上传功能的组件或框架也很多,如基于java的Commons FileUpload.还有Struts1.x和Struts2中带的上传文件功能(实际上,Struts2 ...

  3. 使用 PHP_CodeSniffer 检查 代码 是否 符合 编码规范

    服务端部署:PHP_CodeSniffer HG 服务端部署篇 1.下载PHP_CodeSniffer 前往 https://github.com/squizlabs/PHP_CodeSniffer ...

  4. ASP.NET MVC 4 (九) 模型绑定

    模型绑定指的是MVC从浏览器发送的HTTP请求中为我们创建.NET对象,在HTTP请求和C#间起着桥梁的作用.模型绑定的一个最简单的例子是带参数的控制器action方法,比如我们注册这样的路径映射: ...

  5. SpringBoot自定义错误信息,SpringBoot适配Ajax请求

    SpringBoot自定义错误信息,SpringBoot自定义异常处理类, SpringBoot异常结果处理适配页面及Ajax请求, SpringBoot适配Ajax请求 ============== ...

  6. android程序---->android多线程下载(二)

    上篇我们讲到了android中下载的断点续传问题,今天我们开始学习下载的多线程问题.本次的多线程源码下载:androdi中多线程下载的实现代码.有关断点续传的问题,请参见博客:android程序--- ...

  7. npm更新升级

    更新 npm install npm -g

  8. 深入理解 Java 虚拟机之学习笔记(2)

    本节介绍 Java堆的OutOfMemoryError测试 Eclipse Memory Analyzer分析内存溢出 虚拟机栈和本地方法栈StackOverflowError测试 方法区和运行时常量 ...

  9. 实现类似于QQ空间相册的点击图片放大,再点后缩小回原来位置

    前几天看到了有人在android5.0上实现了如下图一样的效果,我自己就去搜了下.参考了国外一篇文章和国内的一篇文章,最终实现了想要的效果.具体参考的网址我已经贴到文章末尾,大家可以去英文的那个网站看 ...

  10. angular ng-repeat中DOM的重新渲染机制(项目中遇到问题)

    昨天在项目中遇到一个问题:文本框中输入值只要变化就要请求接口获取数据赋值给couponData,这样性能不好.所以和服务器端沟通改成了服务器端一次性返回所有数据,这样前端只要获取一次数据然后保存在对象 ...