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. python web框架简介Bottle Flask Tornado

    Bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Python的标准库外,其不依赖任何其他模块. ? 1 2 3 4 pip inst ...

  2. UNITY 带spriterender的对象导出为prefab时主贴图丢失的BUG

    从场景导出带有sprite的对象为prefab时贴图丢失的BUG.解决方案:对场景中每个sprite重新赋一下贴图,然后导出就好了,原因不明. 补充:这个有时候是因为贴图类型不是 2D AND UI ...

  3. IE11 - Object doesn't support property or method 'includes'

    IE不支持字符串的includes()方法:可以用indexOf()替换: includes()方法返回true和false; var str = "asdklmn": if(st ...

  4. Elasticsearch学习系列之term和match查询实例

    Elasticsearch查询模式 一种是像传递URL参数一样去传递查询语句,被称为简单查询 GET /library/books/_search //查询index为library,type为boo ...

  5. C#中的goto

    int i = 9;if (i % 2 == 0) goto Found;else goto NoFound; NoFound:            Console.WriteLine(i.ToSt ...

  6. Solr集群—SolrCloud

    1.   学习计划 1.solr集群搭建 2.使用solrj管理solr集群 3.把搜索功能切换到集群版 2.   什么是SolrCloud SolrCloud(solr 云)是Solr提供的分布式搜 ...

  7. vuejs 2.0 键盘事件

    <!DOCTYPE html> <html> <head> <title></title> <meta charset="u ...

  8. Hibernate中Session与本地线程绑定

    ------------------siwuxie095 Hibernate 中 Session 与本地线程绑定 1.Session 类似于 JDBC 的连接 Connection 2.Session ...

  9. Appium+python自动化-Remote远程控制

    前言 在第三篇启动app的时候有这样一行代码driver = webdriver.Remote('http://192.168.1.1:4723/wd/hub', desired_caps),很多小伙 ...

  10. Python学习笔记_获取当前目录和上级目录

    实验目标:获取当前目录和上级目录 系统环境: 1.OS:Win10 64位 2.Pythoh 3.7 3.实验路径:C:\Work\Python\MergeExcel 代码参考: # -*- codi ...