总结一下设置图标的三种方式:

(1)button属性:主要用于图标大小要求不高,间隔要求也不高的场合。

(2)background属性:主要用于能够以较大空间显示图标的场合。

(3)drawableLeft属性:主要用于对图标与文字之间的间隔有要求的场合。

注意使用 background 或者 drawableLeft时 要设置 android:button="@null"

监听:

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {

}
});
1
2
3
4
5
6
7
下面是一个例子:
效果图:

布局文件: activity_radio_button.xml 中使用

<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">

<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:button="@null"
android:checked="true"
android:drawablePadding="10dp"
android:drawableTop="@drawable/selback"
android:gravity="center_horizontal"
android:text="男"
android:textColor="#FF0033"
/>

<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:button="@null"
android:drawablePadding="10dp"
android:drawableTop="@drawable/selback"
android:gravity="center_horizontal"
android:text="女"
android:textColor="#000000"
/>

</RadioGroup>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
选择器:selback.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@mipmap/on"/>
<item android:drawable="@mipmap/off"/>
</selector>
1
2
3
4
选择器用到的图片:

RadioButtonActivity 中监听选中

package rolechina.jremm.com.test4;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class RadioButtonActivity extends Activity {
private RadioGroup radioGroup;
private RadioButton radioButton1;
private RadioButton radioButton2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_button);
radioGroup = findViewById(R.id.radioGroup);
radioButton1 = findViewById(R.id.radioButton1);
radioButton2 = findViewById(R.id.radioButton2);

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// 选中 文字 显示红色,没有选中显示黑色
if(radioButton1.isChecked()) {
radioButton1.setTextColor(Color.parseColor("#FF0033"));
}else{
radioButton1.setTextColor(Color.parseColor("#000000"));
}

if(radioButton2.isChecked(http://www.my516.com)) {
radioButton2.setTextColor(Color.parseColor("#FF0033"));
}else{
radioButton2.setTextColor(Color.parseColor("#000000"));
}

}
});
}
}

---------------------

Android中单选框RadioButton的基本用法的更多相关文章

  1. WPF中单选框RadioButton

    单选框RadioButton的基本使用: <StackPanel Margin="10"> <Label FontWeight="Bold"& ...

  2. Android中如何设置RadioButton在文字的右边,图标在左边

    from:http://blog.csdn.net/sunnyfans/article/details/7901592?utm_source=tuicool&utm_medium=referr ...

  3. Android中Cursor类的概念和用法[转]

    首页 > 程序开发 > 移动开发 > Android > 正文   Android中Cursor类的概念和用法 2011-09-07      0个评论       收藏    ...

  4. vue+elementUI中单选框el-radio设置默认值和唯一标识某个单选框

    vue+elementUI中单选框el-radio设置默认值 如果后台返回的单选框的值是number:单选框的lable需要设置成 :lable='0';如下: <el-form-item la ...

  5. 可分组的选择框控件(MVVM下)(Toggle样式 仿造单选框RadioButton,复选框CheckBox功能)

    原地址: http://www.cnblogs.com/yk250/p/5660340.html 效果图如下:支持分组的单选框,复选框样式和MVVM下功能的实现.这是项目中一个快捷键功能的扩展. 1, ...

  6. Android课程---单选框与复选框的实现

    <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="ht ...

  7. 转:Android中的Handler的机制与用法详解

    注:Message类的用法: message的几个参数都可以携带数据,其中arg1与arg2可以携带int类型,what是用户自定义的int型,这样接受者可以了解这个消息的信息. 说明:使用Messa ...

  8. 用css实现html中单选框样式改变

     我们都知道,input的单选框是一个小圆框,不能直接更改样式.但是我们在很多网页中看到的单选框样式可不仅限于默认的那个样式(看上去没啥新意,也比较丑).那么,接下来我将介绍下如何实现该功能. 首先, ...

  9. Android中的Handler的机制与用法详解

    概述: 很多android初学者对android 中的handler不是很明白,其实Google参考了Windows的消息处理机制, 在Android系统中实现了一套类似的消息处理机制.在下面介绍ha ...

随机推荐

  1. PostgreSQL 图形化客户端工具的使用技巧你都get了吗?

    PostgreSQL 数据库作为目前功能较强大的开源数据库,得到了广泛应用.其中,TSA就用到了这款数据库来存储处理后的一些业务数据.虽然PostgreSQL自身提供了命令行交互式客户端工具psql, ...

  2. 【转】Chrome headless 模式

    原文地址: http://www.cnblogs.com/fnng/p/7797839.html 我们在通过Selenium运行自动化测试时,必须要启动浏览器,浏览器的启动与关闭必然会影响执行效率,而 ...

  3. c#操作rabbitmq

    今天将会介绍如果使用rabbitmq进行简单的消息入队,出队操作,因为本文演示的环境要用到上文中配置的环境,所以要运行本文sample,请先按上一篇中完成相应环境配置.         首先,我们下载 ...

  4. Codechef QGRID

    QGRID code 给定一个 n × m(1 <= m <= 3) 的点网格,网格的边上以及点上都有权值.初始时所有点的权值都为 0 .维护两种操作:1. x1 y1 x2 y2 c 把 ...

  5. Codechef LOCAUG17

    做完题目很少有写题解的习惯,强行PO一组吧. 比赛链接:https://www.codechef.com/LOCAUG17 PRINCESS 给定字符串s,问s是否存在长度大于1的回文子串. 解:分两 ...

  6. k8s-StatefulSet控制器-十四

    一.StatefulSet概述 RC.Deployment.DaemonSet都是面向无状态的服务,它们所管理的Pod的IP.名字,启停顺序等都是随机的,而StatefulSet管理所有有状态的服务, ...

  7. js 将json字符串转换为json对象的方法解析-转

    例如: JSON字符串:var str1 = '{ "name": "cxh", "sex": "man" }'; JS ...

  8. 51nod 1347 【水】

    #include<cstdio> #include <map> #include<iostream> #include<string.h> #inclu ...

  9. hdoj5402 【模拟/构造】

    题意: 给你一个矩阵,每个值都是非负,然后让你从左上角走到右下角,每个点只能走一次,求到终点的最大值,还要输出一条路径 思路: 一开始拿到还以为搜索之类的,但是发现神特么暴力+麻烦(因为路径这个东西. ...

  10. update cdh version ,but cdh use old conf ,problem solve

    最近升级cdh版本,从4.5 升级到 5.0.0 beta-2 但是升级后,发现/etc/alternatives 路径下的软链接还是只想旧的4.5 版本,而且hadoop环境也是沿用4.5 的版本c ...