activity_radio_button.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RadioGroup
android:id="@+id/rg_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/rd_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:checked="true"
android:textSize="30sp"
android:textColor="#FF6600"/>
<RadioButton
android:id="@+id/rd_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"
android:textSize="30sp"
android:textColor="#FF6600"/>
</RadioGroup>
<RadioGroup
android:id="@+id/rg_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/rg_1"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rd_3"
android:layout_width="100dp"
android:layout_height="40dp"
android:button="@null"
android:text="男"
android:checked="true"
android:gravity="center"
android:background="@drawable/btn_41"
android:textSize="30sp"
android:textColor="#000000"/>
<RadioButton
android:id="@+id/rd_4"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="女"
android:gravity="center"
android:button="@null"
android:textSize="30sp"
android:background="@drawable/btn_41"
android:textColor="#000000"/>
</RadioGroup>
</RelativeLayout>

 

RadioButtonActivity。java

实现按钮的监听事件

package com.example.helloworld;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast; public class RadioButtonActivity extends AppCompatActivity {
private RadioGroup mRg1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_button);
mRg1= (RadioGroup) findViewById(R.id.rg_1);
mRg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){ @Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
RadioButton radioButton=group.findViewById(checkedId);
Toast.makeText(RadioButtonActivity.this, radioButton.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}

  

效果

RadioButtton的更多相关文章

  1. Android UI-仿微信底部导航栏布局

    现在App基本的标配除了侧滑菜单,还有一个就是底部导航栏,常见的聊天工具QQ,微信,购物App都有底部导航栏,用户可以随便切换看不同的内容,说是情怀也好,用户体验也罢.我们开发的主要的还是讲的是如何如 ...

随机推荐

  1. 字节跳动 iOS Heimdallr 卡死卡顿监控方案与优化之路

    点这里申请 本文主要介绍Heimdallr对卡死.卡顿异常的监控原理,并结合长时间的业务沉淀发现的问题进行不断迭代和优化,逐步实现全面.稳定.可靠的历程. 作者:字节跳动终端技术--白昆仑 前言 卡死 ...

  2. Spring源码-IOC部分-容器简介【1】

    实验环境:spring-framework-5.0.2.jdk8.gradle4.3.1 Spring源码-IOC部分-容器简介[1] Spring源码-IOC部分-容器初始化过程[2] Spring ...

  3. 使用Xamarin开发移动应用示例——数独游戏(六)使用数据库

    项目代码可以从Github下载:https://github.com/zhenl/ZL.Shudu .代码随项目进度更新. 现在我们希望为应用增加更多的功能,比如记录每个完成的游戏,可以让用户自己添加 ...

  4. Redis 源码简洁剖析 05 - ziplist 压缩列表

    ziplist 是什么 Redis 哪些数据结构使用了 ziplist? ziplist 特点 优点 缺点 ziplist 数据结构 ziplist 节点 pre_entry_length encod ...

  5. switch多选择结构

    switch多选择结构 多选择结构还有一个实现方式就是switch case语句. switch case 语句判断一个变量与一个系列值中某个值是否相等,每个值称为一个分支. 语法: switch(e ...

  6. asp.net core 中的各种路径

    1.获取完整网址URL 方法一:先引用"using Microsoft.AspNetCore.Http.Extensions;",然后直接用"Request.GetDis ...

  7. js 对象的深克隆

    前端笔试或者面试的时候,很喜欢问的一个问题就是对象的深度克隆,或者说是对象的深度复制.其实这个问题说容易很容易,但是要说全面也挺不易. 要弄明白对象的克隆,首先要明白js中对象的组成.在js中一切实例 ...

  8. C++中的常见错误

    1.变量定义位置错误 1 int sum = 0; 2 3 do 4 { 5 int i = 1; 6 sum += i; 7 i++; 8 }while(i <= 100);//错误:i没有定 ...

  9. 隐藏键盘的N种方法

    ---Created by luo.h 显示键盘 [textField becomeFirstResponder]; 隐藏键盘 @interface ViewController ()<UITe ...

  10. NoSQL之Redis配置与优化

    NoSQL之Redis配置与优化 目录 NoSQL之Redis配置与优化 一.关系数据库和非关系数据库 1. 关系型数据库 2. 非关系型数据库 3. 非关系型数据库产生背景 4. 关系型数据库和非关 ...