RadioGroup的使用
前言:
使用RadioGroup就可以在选择情况多的时候,简化代码
RadioGroup
使用互斥选择时,会使用RadioGroup标签下面RadioButton,如下面的代码:(这样写下来,男和女的选择时垂直的,要想水平的话可以在RadioGroup中添加android:orientation="horizontal")
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:id="@+id/radiogroup">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/nan"
android:text="男"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/nv"
android:text="女"/>
</RadioGroup>
在Java中的代码是
public class MainActivity extends AppCompatActivity {
private RadioGroup radiogroup;
private RadioButton nan,nv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nan=(RadioButton)findViewById(R.id.nan);
nv=(RadioButton)findViewById(R.id.nv);
radiogroup=(RadioGroup)findViewById(R.id.radiogroup);
radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkId) {
switch (checkId){
case R.id.nan:
Toast.makeText(MainActivity.this,"你选择的是:"+nan.getText(),Toast.LENGTH_SHORT).show();
break;
case R.id.nv:
Toast.makeText(MainActivity.this,"你选择的是:"+nv.getText(),Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
});
}
}
此时的setOnCheckedChangeListener(设置监听器) OnCheckedChangeListener(监听器) onCheckedChanged(抽象方法)和RadioButton中的名称一样,但是上面的哪些是在RadioGroup中的,而且onCheckedChanged(抽象方法)不同,RadioGroup下的是
public void onCheckedChanged(RadioGroup group, int checkId) {}
RadioButton下的是
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){}
方法的名称相同,但是参数不同
RadioGroup的使用的更多相关文章
- Android RadioGroup和RadioButton详解
实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用.RadioGroup是单选组合框,可以容纳多个RadioButton的容器.在没有RadioGrou ...
- RadioGroup 和 ViewPager 绑定 实现切换
package com.jereh.helloworld.activity.day12; import android.app.Activity; import android.os.Bundle; ...
- RadioGroup实现导航栏
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
- ViewPager+RadioGroup实现标题栏切换,Fragment切换
1.说明: 在使用RadioGroup做标题栏切换的时候,跟ViewPager的滑动有冲突,最后查看了源码+断点调试解决了一些碰到的问题,写一篇博客总结一下,有同样需求的朋友可以借鉴一下,自己以后有用 ...
- RadioGroup、RadioButton、CheckBox、Toast用法
xml布局文件如下: <RadioGroup android:id="@+id/sex" android:layout_width="wrap_content&qu ...
- CompoundButton.OnCheckedChangeListener与RadioGroup.OnCheckedChangeListener冲突
当RadioGroup与CompoundButton同时存在且都要监听事件时CompoundButton.OnCheckedChangeListener与RadioGroup.OnCheckedCha ...
- Fragment配合RadioGroup实现点击切换布局
这里用了 compile 'com.jakewharton:butterknife:7.0.1' compile 'org.greenrobot:eventbus:3.0.0' MainActivit ...
- android radiogroup样式(设置切换背景与文字颜色)
main.xml <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_cont ...
- Android入门(八):使用RadioGroup 和RadioButton组件建立单选清单
这一章,我们学习RadioGroup 和RadioButton组件,我们新建一个项目,编码过程与前几章的项目类似. 1.建立字符串资源文件strings.xml: <resources> ...
- Android自定义控件----RadioGroup实现APP首页底部Tab的切换
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
随机推荐
- Spring Cloud常用注解
@SpringBootApplication(exclude = {org.springframework.boot.autoconfigure.security.servlet.SecurityAu ...
- windows通过zip安装mysql5.7.26的一个坑
需要将my.ini的 红框的/不能写成\ 注意编码格式问题 然后 mysqld --initialize-insecure mysqld --install net start mysql
- node.js绑定监听事件EventEmitter类
Node.js 有多个内置的事件,我们可以通过引入 events 模块,并通过实例化 EventEmitter 类来绑定和监听事件,如下: // 引入 events 模块 var events = r ...
- AtCoder agc007_d Shik and Game
洛谷题目页面传送门 & AtCoder题目页面传送门 有\(1\)根数轴,Shik初始在位置\(0\).数轴上有\(n\)只小熊,第\(i\)只在位置\(a_i\).Shik每秒可以向左移动\ ...
- 9.ActiveMQ
1. ActiveMQ 安装 下载并安装ActiveMQ服务器端(1.1):从http://activemq.apache.org/download.html下载最新的ActiveMQ(1.2):直接 ...
- NoSQL 是什么
NoSQL 全称 Not only SQL ,是一种相对较新的数据库设计方式,传统的关系型数据库使用的是固定模式,并将数据分割在多个表中,然而,对于大数据集的情况,数据量太大使其难以存放在单一的服务器 ...
- Ubuntu 16.04 编译安装&&用dpkg安装--zabbix3.4
编译安装zabbix3.4,官方有教程,但是遇到一些报错: 总结下来,需要安装这些环境: 更新系统: apt update apt upgrade 先装这些: apt-get install libm ...
- gerrit关闭管理员权限后解决办法
问题描述:gerrit以管理员的身份登录后,create new list和 create new group不显示,导致无法创建新的项目和权限组 出现问题:gerrit可视化页面误删all_proj ...
- SQL语言的四种类型
SQL语言共分为四大类:数据查询语言DQL,数据操纵语言DML,数据定义语言DDL,数据控制语言DCL. 1. 数据查询语言DQL 数据查询语言DQL基本结构是由SELECT子句,FROM子句,WHE ...
- java学习-初级入门-面向对象①-面向对象概述-结构化程序设计
为了学习面向对象程序设计,今天我们先利用面向对象以前的知识,设计一个学生类. 要求进行结构化程序设计. 学生类: Student 要求:存储学生的基本信息(姓名.性别.学历层次和年级),实现学生信息的 ...