1、相关简介

  RadioButton需要和RadioGroup结合使用,在RadioGroup设置布局方式!

  Checkbox是单独使用,本文为了方便放在了RadioGroup中!

2、简单使用

  方法说明:

final RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
radioGroup.getChildCount() //获取子控件数量
RadioButton radioButton = (RadioButton)radioGroup.getChildAt(i); //获取子控件
radioButton.isChecked() //获取按钮选择状态
radioButton.getText() //获取按钮文本

  xml布局文件

        <RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioGroup"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radio11"
android:text="男"
android:checked="true"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radio22"
android:text="女"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radio33"
android:text="太监"/>
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioBtn"
android:text="提交"/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/boxGroup"
android:orientation="vertical">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/box11"
android:text="男"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/box22"
android:text="女"/>
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/boxBtn"
android:text="提交"/>

  Java文件:

    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// Set up the login form. final RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
// 方法一
// radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
// @Override
// public void onCheckedChanged(RadioGroup radioGroup, int i) {
// RadioButton radioButton = (RadioButton)findViewById(i);
// Toast.makeText(getApplicationContext(),"你选择了"+radioButton.getText(),Toast.LENGTH_LONG).show();
// }
// }); // 方法二
Button button = (Button)findViewById(R.id.radioBtn);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
for (int i = 0;i < radioGroup.getChildCount();i++){
RadioButton radioButton = (RadioButton)radioGroup.getChildAt(i);
if (radioButton.isChecked()){
Toast.makeText(getApplicationContext(),"你选择了"+radioButton.getText(),Toast.LENGTH_SHORT).show(); }
}
}
}); Button button1 = (Button)findViewById(R.id.boxBtn);
final RadioGroup boxGroup = (RadioGroup)findViewById(R.id.boxGroup);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
String choos = "";
for (int i=0;i<boxGroup.getChildCount();i++){
CheckBox checkBox = (CheckBox)boxGroup.getChildAt(i);
if (checkBox.isChecked()){
choos +=checkBox.getText().toString();
}else {
if (choos.indexOf(checkBox.getText().toString())!=-1){
choos.substring(choos.indexOf(checkBox.getText().toString()));
}
}
if (i==boxGroup.getChildCount()-1){
Toast.makeText(getApplicationContext(),"选了"+choos,Toast.LENGTH_SHORT).show();
}
}
}
}); }
}

效果图

Android基础控件单选按钮RadioButton和Checkbox复选按钮的使用的更多相关文章

  1. Android基础控件ListView基础操作

    1.简介 基于Android基础控件ListView和自定义BaseAdapter适配器情况下,对ListView的数据删除和添加操作: public boolean add(E e) {//添加数据 ...

  2. android基础控件的使用

    控件在屏幕上位置的确定 通常情况下控件在屏幕上确定至少要连接两条线(一条水平,一条垂直) 如下图连接了四条线 辅助线 辅助线的调出: 水平辅助线:进入activity.xml的设计模式之后如下图 为了 ...

  3. android 基础控件(EditView、SeekBar等)的属性及使用方法

        android提供了大量的UI控件,本文将介绍TextView.ImageView.Button.EditView.ProgressBar.SeekBar.ScrollView.WebView ...

  4. 矩阵, 矩阵 , Android基础控件之ImageView

    天下文章大家抄,以下所有内容,有来自copy,有来自查询,亦有自己的总结(目的是总结出自己的东西),所以说原创,不合适,说是转载也不恰当,所以我称之为笔记,可惜没有此分类选项,姑且不要脸一点,选择为原 ...

  5. android基本控件学习-----RadioButton&CheckBox

    RadioButton(单选框)和CheckBox(复选框)讲解: 一.基本用法和事件处理 (1)RadioButton单选框,就是只能选择其中的一个,我们在使用的时候需要将RadioButton放到 ...

  6. android 基础控件 EditText

    EditText 简介: EditText 控件继承 TextView ,它有TextView的所有属性和方法,并且自身是可编辑的: extends TextView java.lang.Object ...

  7. Android 基础控件 TextView

    一TextView介绍: TextView是UI最基本的组件,使用TextView可以显示丰富的文本信息.设置添加TextView最常见的方法就是在xml中添加TextView元素,并指定属性.Tex ...

  8. Android基础控件TextClock和Chronometer的使用

    1.简介 DigitalClock, TextClock,AnalogClock,Chronometer其中DigitalClock和AnalogClock废弃了! TextClock是在Androi ...

  9. Android基础控件ProgressBar进度条的使用

    1.简介 ProgressBar继承与View类,直接子类有AbsSeekBar和ContentLoadingProgressBar, 其中AbsSeekBar的子类有SeekBar和RatingBa ...

随机推荐

  1. DRF的三大认证组件

    目录 DRF的三大认证组件 认证组件 工作原理 实现 权限组件 工作原理 实现 频率组件 工作原理 实现 三种组件的配置 DRF的三大认证组件 认证组件 工作原理 首先,认证组件是基于BaseAuth ...

  2. Qt学习笔记----基础知识

    一.qt的本质 qt的本质是c++的图形界面类库,本身是mvc结构.qt能火最大程度 归功于它跨平台的特性,一次编码,多次编译应用. 注意:qt由于历史原因,经历了奇趣.诺基亚.digit公司,导致q ...

  3. C# 调用java的Webservice时关于非string类型处理

    比如webservice地址是:http://wdft.com:80/services/getOrderService1.0?wsdl 方法是:getOrder 1.首先添加引用: 2. 3.引用完成 ...

  4. elasticsearch.net一个查询问题

    .Query(q => q.Bool(b => b.Must(m => m.MultiMatch(t => t .Fields(f => f.Field(obj => ...

  5. css实现单行、多行文本溢出显示省略号(…)

    一.单行文本溢出显示省略号(…) 省略号在ie中可以使用text-overflow:ellipsis了,但有很多的浏览器都需要固定宽度了,同时ff这些浏览器并不支持text-overflow:elli ...

  6. [JZOJ6347] 【NOIP2019模拟2019.9.8】ZYB玩字符串

    题目 题目大意 有一个字符串\(p\).一开始字符串\(s\)为空串. 接下来进行若干次操作:在\(s\)的某个空隙中插入\(p\). 给出操作后的\(s\),问长度最小的\(p\). 思考历程 感觉 ...

  7. POJ 2104:K-th Number 整体二分

    感觉整体二分是个很有趣的东西. 在别人的博客上看到一句话 对于二分能够解决的询问,如果有多个,那么如果支持离线处理的话,那么就可以使用整体二分了 树套树写了一天还是WA着,调得焦头烂额,所以决定学cd ...

  8. SSH整合时执行hibernate查询报错:java.lang.ClassCastException: com.ch.hibernate.Depart

    今天在整合ssh三个框架时,有一个功能,是查询所有员工信息,且员工表和部门表是多对一的映射关系,代码能正常运行到查询得到一个List集合,但在页面展示的时候,就报异常了, java.lang.Clas ...

  9. Django杂篇(1)

    目录 Django杂篇(1) bulk_create Pagination 创建多对多表关系的常用方法 form校验组件的应用 渲染页面 展示错误信息 校验数据 常用字段 Django杂篇(1) 这里 ...

  10. HYNB Round 15: PKU Campus 2019

    HYNB Round 15: PKU Campus 2019 C. Parade 题意 将平面上n*2个点安排在长度为n的两行上. 做法 首先可以忽略每个点之间的影响,只用考虑匹配即可 然后把所以点归 ...