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. 二、jenkins配置email(以腾讯企业qq为例)

    废话不多说,直接上干货: 主要针对两个部分进行介绍: 1.jenkins内置的邮件功能: 2.Editable Email Notification插件的邮件功能: 低版本的jenkins有很多插件都 ...

  2. django -- 自定义simpletag 和 filter

    django中自定义simpletag,即使用自己定义的函数在html中处理数据. 步骤: 1.创建并注册app settings.py INSTALLED_APPS = [ 'django.cont ...

  3. 【读书笔记】 DevOps实践 - 驭DevOps之力强化技术栈并优化IT运行

    读书小结 DevOps实践 - 驭DevOps之力强化技术栈并优化IT运行 这本书共200页,读完大概三天:(我指的不是fulltime的一天,而是工作时间以外的一天) 本书是参加16年QConf开发 ...

  4. web开发-CORS支持

    一.简介 Web 开发经常会遇到跨域问题,解决方案有:jsonp,iframe,CORS 等等 1.1.CORS与JSONP相比 1.JSONP只能实现GET请求,而CORS支持所有类型的HTTP请求 ...

  5. Bootstrap 与 Jquery validate 结合使用——多个规则实现

    进行开发的时候,遇到了需要有多个规则来校验,如新用户过来一套校验规则,老用户过来又是一套规则,这时候就要需要定义多套校验规则. 首先要熟悉Bootstrap和Jquery validate的使用,详情 ...

  6. H264码流中SPS PPS详解<转>

    转载地址:https://zhuanlan.zhihu.com/p/27896239 1 SPS和PPS从何处而来? 2 SPS和PPS中的每个参数起什么作用? 3 如何解析SDP中包含的H.264的 ...

  7. JBPM4.4学习笔记

    1.JBPM4表说明: JBPM4_DEPLOYMENT 流程定义表 JBPM4_DEPLOYPROP 流程定义属性表 JBPM4_EXECUTION 流程实例表 JBPM4_HIST_ACTINST ...

  8. C++ const引用

    (1)       在实际的程序中,引用主要被用做函数的形式参数--通常将类对象传递给一个函数.引用必须初始化. 但是用对象的地址初始化引用是错误的,我们可以定义一个指针引用. 1 int ival ...

  9. Python Windows下打包成exe文件

    Python Windows 下打包成exe文件,使用PyInstaller 软件环境: 1.OS:Win10 64 位 2.Python 3.7 3.安装PyInstaller 先检查是否已安装Py ...

  10. Windows查看端口占用及杀掉进程

    "到底是谁占用了80端口,该怎么查,怎么终止它?",这里就简单讲解一下,在windows下如何查看80端口占用情况?是被哪个进程占用?如何终止等.这里主要是用到windows下的D ...