1.控件RadioButton需要用RadioGroup包裹起来,才能使用
2.RadioButton必须设置ID才能实现单选功能
3.RadioGroup有方向(垂直方向 和 水平方向)默认是垂直方向 先介绍原生的RadioButton,然后再介绍自定义效果RadioButton Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"> <!-- RadioGroup RadioButton -->
<RadioGroup
android:id="@+id/rg_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"> <RadioButton
android:id="@+id/rb_man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
/> <RadioButton
android:id="@+id/rb_woman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"
/> </RadioGroup> </LinearLayout>

监听事件:

package liudeli.ui.all;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast; public class WidgetActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_widget); // RadioButton
RadioGroup rgSex = findViewById(R.id.rg_sex);
RadioButton rb_man = findViewById(R.id.rb_man);
RadioButton rb_woman = findViewById(R.id.rb_woman); // 默认设置为男选中状态
rb_man.setChecked(true); rgSex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.rb_man:
alertToast("男");
break;
case R.id.rb_woman:
alertToast("女");
break;
}
}
}); } private void alertToast(String text) {
Toast.makeText(this, text, Toast.LENGTH_LONG).show();
} @Override
protected void onDestroy() {
super.onDestroy();
}
}


自定义RadioButton效果:


RadioButton使用的选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/radio_false" android:state_checked="false" /> <item android:drawable="@drawable/radio_true" android:state_checked="true" /> <!-- 注意⚠:选择器如果的默认一定要放在最后一行,否则会出现莫名其妙的问题 -->
<!--<item android:drawable="@drawable/radio_false" />--> </selector>

RadioButton 设置:

         android:button="@null" 去除RadioButton圆点

           android:background="@drawable/radio_button_selector" 设置选择器效果

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"> <!--
RadioGroup RadioButton
-->
<RadioGroup
android:id="@+id/rg_sex"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="10dp"> <!--
android:button="@null" 去除RadioButton圆点
android:background="@drawable/radio_button_selector" 设置选择器效果
-->
<RadioButton
android:id="@+id/rb_man"
android:layout_width="30dp"
android:layout_height="30dp" android:button="@null"
android:background="@drawable/radio_button_selector"
/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:layout_marginLeft="10dp"
/> <RadioButton
android:id="@+id/rb_woman"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="60dp" android:button="@null"
android:background="@drawable/radio_button_selector"
/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"
android:layout_marginLeft="10dp"
/> </RadioGroup> </LinearLayout>
												

Android-自定义RadioButton的更多相关文章

  1. Android 自定义RadioButton的样式

    Android 自定义RadioButton的样式 我们知道Android控件里的button,listview可以用xml的样式自定义成自己希望的漂亮样式. 最近用到RadioButton,利用xm ...

  2. Android 自定义RadioButton实现

    由于使用小米系统MIUI运行是RadioButton样式跟google Android API自定义的不一样,则我们可以定义任何想要的东东.没有做不到,只有想不到 Android 自定义RadioBu ...

  3. android自定义radiobutton样式文字颜色随选中状态而改变

    主要是写一个 color selector 在res/建一个文件夹取名color res/color/color_radiobutton.xml <selector xmlns:android= ...

  4. android 自定义 radiobutton 文字颜色随选中状态而改变

    主要是写一个 color selector 在res/建一个文件夹取名color res/color/color_radiobutton.xml <?xml version="1.0& ...

  5. Android自定义radiobutton(文字靠左,选框靠右)

    <RadioButton android:id="@+id/rb_never" android:layout_width="fill_parent" an ...

  6. 转:android 自定义RadioButton样式

    http://gundumw100.iteye.com/blog/1146527  上面这种3选1的效果如何做呢?用代码写? 其实有更简单的办法,忘了RadioButton有什么特性了吗? 我就用Ra ...

  7. Android 自定义 radiobutton

    <RadioButton android:id="@+id/radiobutton_pay_method" android:layout_width="30dp&q ...

  8. Android 自定义RadioButton样式

     上面这种3选1的效果如何做呢?用代码写? 其实有更简单的办法,忘了RadioButton有什么特性了吗? 我就用RadioButton实现了如上效果,其实很简单的. 首先定义一张background ...

  9. React Native之(支持iOS与Android)自定义单选按钮(RadioGroup,RadioButton)

    React Native之(支持iOS与Android)自定义单选按钮(RadioGroup,RadioButton) 一,需求与简单介绍 在开发项目时发现RN没有给提供RadioButton和Rad ...

  10. android自定义RadioGroup实现可以添加多种布局

    android自带的RadioGroup是继承自LinearLayout,如果布局的时候不是直接写radiobutton,即radiobutton外面还包了一层容器,这时分组是不成功的,因为查找不到r ...

随机推荐

  1. 关于git的reset、checkout、revert

    https://www.atlassian.com/git/tutorials/resetting-checking-out-and-reverting/file-level-operations 最 ...

  2. ansible初识二

    一.ansible模块(yum.pip.service.conr.user.group) 上篇中我们已经学了ansible 的几个模块, 接下来再来学习几个, 那么你是否知道ansible 一共有多少 ...

  3. 摆脱Login控件,自己定义登录操作

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e) { //在登录过程中,程序自动使用login.aspx进 ...

  4. Redis实战——安装

    借鉴来源:https://www.cnblogs.com/codersay/p/4301677.html redis官网地址:http://www.redis.io/ 最新版本: redis-4.0. ...

  5. docker redis4.0 集群(cluster)搭建

    前言 redis集群对于很多人来说非常熟悉,在前些日子,我也有一位大兄弟也发布过一篇关于在阿里云(centOS7)上搭建redis 集群的文章,虽然集群搭建的文章在网上很多,我比较喜欢这篇文章的地方是 ...

  6. avalon1.3的新特性预览

    avalon1.2的性能优化风暴很快就告一段落,入职也快一个月了,许多乱七八糟的事也少了下来,估计未来一个月会有许多好东呈现给大家. 首先是一个性能检测工具.由于MVVM是将原本由人脑干的事,转到各种 ...

  7. 游戏动作师使用Unity3D遇到过的所有问题

    http://blog.csdn.net/onafioo/article/details/50865169 http://www.gameres.com/thread_480489.html 文/拉撒 ...

  8. Windows 安装 Maven 及 Eclipse 安装Maven插件

    环境说明: window 8.1 64bit Eclipse Version: Luna Release (4.4.0) Maven 3.0.5 Windows Maven 安装: 1.确保安装了JD ...

  9. Selenium自动化测试WebDriver下载

    1. 所有版本chrome下载 是不是很难找到老版本的chrome?博主收集了几个下载chrome老版本的网站,其中哪个下载的是原版的就不得而知了. http://www.slimjet.com/ch ...

  10. Redis初学笔记

    1.官网概述 Redis is an open source (BSD licensed), in-memory data structure store, used as database, cac ...